출처: http://k-flow.tistory.com/trackback/8
Java에서 Json-lib를 사용하기 위해서 필요한 라이브러리_
eclipse 사용시 Build Path -> Add Exeternal Archives 에 추가
json-lib-2.1-jdk15.jar
jakarta commons-lang 2.3
jakarta commons-beanutils 1.7.0
jakarta commons-collections 3.2
jakarta commons-logging 1.1
ezmorph 1.0.4
Source Code
json형식 ==> {"jsontest":[{"name":"Mr.Cho","location":"Seoul"}]}
import java.util.Iterator;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JSONParse {
public static void main(String[] ar) {
String s = "{\"jsontest\":[{\"name\":\"Mr.Cho\",\"location\":\"Seoul\"}]}";
JSONObject jo = JSONObject.fromObject(s);
JSONArray ja = jo.getJSONArray("jsontest");
for (Iterator i = ja.iterator(); i.hasNext();) {
JSONObject ob = (JSONObject) i.next();
String a = ob.getString("name");
String b = ob.getString("location");
System.out.println(a);
System.out.println(b);
}
}
}