由于项目中遇到了这个问题,所以在这里记录一下。
比如:请求到的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");
时间: 2024-10-13 06:19:46