1. 程式人生 > >叠代解析JSON簡單實例

叠代解析JSON簡單實例

hot col sonar clas reat etime ces lis cnblogs

由於項目中遇到了這個問題,所以在這裏記錄一下。

比如:請求到的JSON串:

 1 {
 2     "msg":"數據獲取成功",
 3     "success":true,
 4     "data":[
 5         {
 6             "id":40,
 7             "name":"美術",
 8             "parentId":4,
 9             "photoUrl":"[email protected]",
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/[email protected]",
22 "enable":1, 23 "createTime":1508498216000 24 } 25 ] 26 }, 27 { 28 "id":41, 29 "name":"書法", 30 "parentId":4, 31 "photoUrl":"[email protected]", 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/[email protected]", 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");

叠代解析JSON簡單實例