您的位置:首页 > 移动开发 > Objective-C

使用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));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: