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

来源:互联网 发布:psp动作游戏知乎 编辑:程序博客网 时间:2024/05/10 10:43

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

原创粉丝点击