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

时间:2021-08-03 19:17:07

使用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));}