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

使用json-lib遍历数组与对象

//遍历json数组
String json1 = "{data:[{name:'Wallace'},{name:'Grommit'}]}";
jsonObjSplit = new JSONObject(json1);
JSONArray ja = jsonObjSplit.getJSONArray("data");
for (int i = 0; i < ja.length(); 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)); }

updatedupdated2024-11-302024-11-30