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

android 读取json数据(遍历JSONObject和JSONArray)(转)

2014-05-06 11:44 459 查看
public String getJson(){

String jsonString = "{\"FLAG\":\"flag\",\"MESSAGE\":\"SUCCESS\",\"name\":[{\"name\":\"jack\"},{\"name\":\"lucy\"}]}";//json字符串

try {

JSONObject result = new JSONObject(jsonstring);//转换为JSONObject

int num = result.length();

JSONArray nameList = result.getJSONArray("name");//获取JSONArray

int length = nameList.length();

String aa = "";

for(int i = 0; i < length; i++){//遍历JSONArray

Log.d("debugTest",Integer.toString(i));

JSONObject oj = nameList.getJSONObject(i);

aa = aa + oj.getString("name")+"|";

}

Iterator<?> it = result.keys();

String aa2 = "";

String bb2 = null;

while(it.hasNext()){//遍历JSONObject

bb2 = (String) it.next().toString();

aa2 = aa2 + result.getString(bb2);

}

return aa;

} catch (JSONException e) {

throw new RuntimeException(e);

}

}



原文: http://zhaojianping.blog.51cto.com/725123/629526
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: