迭代解析JSON简单实例

由于项目中遇到了这个问题,所以在这里记录一下。

比如:请求到的JSON串:

 {
"msg":"数据获取成功",
"success":true,
"data":[
{
"id":40,
"name":"美术",
"parentId":4,
"photoUrl":"art@2x.png",
"enable":1,
"pos":2,
"visitUrl":"sysPxbPage",
"ord":0,
"related":"1",
"moduleType":"",
"cateList":[
{
"id":155,
"moduleId":40,
"name":"雕塑",
"photoUrl":"sysPxbPage/artPage/ds@2x.png",
"enable":1,
"createTime":1508498216000
}
]
},
{
"id":41,
"name":"书法",
"parentId":4,
"photoUrl":"writing@2x.png",
"enable":1,
"pos":2,
"visitUrl":"sysPxbPage",
"ord":0,
"related":"1",
"moduleType":"",
"cateList":[
{
"id":158,
"moduleId":41,
"name":"纂刻",
"photoUrl":"sysPxbPage/writingPage/zk@2x.png",
"enable":1,
"createTime":1508498326000
}
]
}
}

比如:获取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");

上一篇:echarts 应用数个样例


下一篇:Centos安装Memcached和(Nginx)Memcache扩展详细教程