迭代解析JSON简单实例
由于项目中遇到了这个问题,所以在这里记录一下。
比如:请求到的JSON串:
1 { 2 "msg":"数据获取成功", 3 "success":true, 4 "data":[ 5 { 6 "id":40, 7 "name":"美术", 8 "parentId":4, 9 "photoUrl":"art@2x.png", 10 "enable":1, 11 "pos":2, 12 "visitUrl":"sysPxbPage", 13 "ord":0, 14 "related":"1", 15 "moduleType":"", 16 "cateList":[ 17 { 18 "id":155, 19 "moduleId":40, 20 "name":"雕塑", 21 "photoUrl":"sysPxbPage/artPage/ds@2x.png", 22 "enable":1, 23 "createTime":1508498216000 24 } 25 ] 26 }, 27 { 28 "id":41, 29 "name":"书法", 30 "parentId":4, 31 "photoUrl":"writing@2x.png", 32 "enable":1, 33 "pos":2, 34 "visitUrl":"sysPxbPage", 35 "ord":0, 36 "related":"1", 37 "moduleType":"", 38 "cateList":[ 39 { 40 "id":158, 41 "moduleId":41, 42 "name":"纂刻", 43 "photoUrl":"sysPxbPage/writingPage/zk@2x.png", 44 "enable":1, 45 "createTime":1508498326000 46 } 47 ] 48 } 49 }
比如:获取photoUrl的值:
JSONObject da = resultJson.getJSONObject("data");
JSONArray d = da .JSONArray("cateList");
JSONObject ob = (JSONObject)d.get(0); // 这里是得到第一个对象
String result = ob.getString("photoUrl");
若是获取success的值:
resultJson.getBooleanValue("success");