使用json-lib遍历数组与对象(JSONArray与JSONObject)
2011-04-12 10:25
711 查看
使用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));
}
//遍历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));
}
相关文章推荐
- android JSONArray JSONObject 字符串转json数组 遍历json对象
- 使用json-lib将Java对象转SONObject,Java集合转JSONArray
- web服务端使用JSONArrayy或JSONObject转json字符串为Java对象或数组
- JSONObject和JSONArray遍历数组与对象
- 使用json-lib将JSONObject转Java对象,JSONArray转Java集合
- 使用json_decode()转换返回数组或对象时出现:Cannot use object of type stdClass as array
- JSONObject和JSONArray遍历数组与对象
- json-lib使用,JSONObject和JSONArray
- 使用json-lib JSONObject JSONArray
- Json-lib使用——JSONObject与JSONArray
- json对象、json格式字符串、java对象【使用此net.sf.json.JSONObject将java对象[比如数组集合Map、Map扩展]转换成json格式字符串】
- 使用json-lib jsonObject 把json 转换成 java对象
- Json-lib使用——JSONObject与JSONArray
- java JSONObject JSONArray对象使用小记
- JSONObject和JSONArray遍历数组与对象
- json-lib使用,JSONObject和JSONArray
- json-lib使用,JSONObject和JSONArray
- json-lib使用,JSONObject和JSONArray
- 数组Array、对象Object、json格式常用的方法小结。
- 【使用反射判断一个Object对象是不是数组,并且遍历数组】