使用json-lib遍历数组与对象(JSONArray与JSONObject)

阅读更多
//遍历json数组
String json1 = "{data:[{name:'Wallace'},{name:'Grommit'}]}";
JSONObject jsonObjSplit = JSONObject.fromObject(json1);
JSONArray ja = jsonObjSplit.getJSONArray("data");
for (int i = 0; i < ja.size(); i++) {
	JSONObject jo = (JSONObject) ja.get(i);
	 System.out.println(jo.get("name"));
}
 
//JSONObject遍历json对象
String json2 = "{name:'Wallace',age:15}";
jsonObj = new JSONObject(json2);
 
for (Iterator iter = jsonObj.keys(); iter.hasNext();) {
String key = (String)iter.next();
System.out.println(jsonObj .getString(key));
}
需要用到的jar包列表:
json-lib-2.2.3-jdk15.jar
jakarta commons-lang 2.4 
jakarta commons-beanutils 1.7.0 
jakarta commons-collections 3.2 
jakarta commons-logging 1.1.1 
ezmorph 1.0.6 
转自:http://fatkun.com/2010/07/jsonarray-jsonobject-array.html

你可能感兴趣的:(json,HTML)