怎么样可以用最简单的方式把下面json中“cargo”子节点中的数据转成数组

酸奶瓶盖儿 发布于 2016/05/24 10:56
阅读 298
收藏 0

@wenshao 你好,想跟你请教个问题:

"loc":
{"C20160500019":
{"address":"上海市徐汇区习勤路-冠生园路交叉路口附近","datetime":"2016-02-26 16:40:23","_id":"C20160500019","lat":31.16199,"lng":121.42697,"flag":3},
"C20160400217":
{"address":"上海市徐汇区柳州路徐汇区漕宝路上海应用技术大学(徐汇校区)附近","datetime":"2016-05-18 14:29:11","_id":"C20160400217","lat":31.16667,"lng":121.42461,"flag":1},
"C20160500001":
{"address":"上海市徐汇区习勤路-冠生园路交叉路口附近","datetime":"2016-02-26 16:40:23","_id":"C20160500001","lat":31.16199,"lng":121.42697,"flag":4},
"C201602170003":
{"address":"上海市徐汇区;柳州路-康健路交叉路口;康健大楼,美宇宾馆柳州路店,康健大楼(南门)附近","datetime":"2016-02-19 11:18:06","_id":"C201602170003","lat":31.230173789112,"lng":121.20448847552,"flag":7},
"C20160500023":
{"address":"上海市徐汇区习勤路-冠生园路交叉路口附近","datetime":"2016-02-26 16:40:23","_id":"C20160500023","lat":31.16199,"lng":121.42697,"flag":1},
"C20160500017":
{"address":"上海市徐汇区习勤路-冠生园路交叉路口附近","datetime":"2016-02-26 16:40:23","_id":"C20160500017","lat":31.16199,"lng":121.42697,"flag":1},
"C20160500008":
{"address":"上海市徐汇区习勤路-冠生园路交叉路口附近","datetime":"2016-02-26 16:40:23","_id":"C20160500008","lat":31.16199,"lng":121.42697,"flag":1}
},
"cargo":[
{"ord_id":"S0003","ord_info":"箱子","sn":"C201602170003","dev_id":"201404290001"},
{"ord_id":"8089","ord_info":"444444","sn":"C20160400217","dev_id":"201602140001"},
{"ord_id":"8089","ord_info":"444444","sn":"C20160400232","dev_id":"201602140002"},
{"ord_id":"8089","ord_info":"444444","sn":"C20160400234","dev_id":"201602140005"},
{"ord_id":"3535","ord_info":"3535","sn":"C20160500001","dev_id":"3535"},
{"ord_id":"45679","ord_info":"455","sn":"C20160500004","dev_id":""},
{"ord_id":"123456","ord_info":"","sn":"C20160500005","dev_id":""},
{"ord_id":"123456","ord_info":"","sn":"C20160500006","dev_id":""},
{"ord_id":"123456","ord_info":"","sn":"C20160500007","dev_id":"201602140002"},
{"ord_id":"123456","ord_info":"","sn":"C20160500008","dev_id":""},
{"ord_id":"32567","ord_info":"","sn":"C20160500017","dev_id":"201602140005"},
{"ord_id":"345789","ord_info":"","sn":"C20160500019","dev_id":""},
{"ord_id":"000222","ord_info":"蛋糕房","sn":"C20160500023","dev_id":"201404290001"}
]
}

加载中
0
风雨中的灰灰
风雨中的灰灰
String str = "{\"loc\":{\"C20160500019\":{\"address\":\"上海市徐汇区习勤路-冠生园路交叉路口附近\",\"datetime\":\"2016-02-26 16:40:23\",\"_id\":\"C20160500019\",\"lat\":31.16199,\"lng\":121.42697,\"flag\":3},\"cargo\":[{\"ord_id\":\"S0003\",\"ord_info\":\"箱子\",\"sn\":\"C201602170003\",\"dev_id\":\"201404290001\"},{\"ord_id\":\"8089\",\"ord_info\":\"444444\",\"sn\":\"C20160400217\",\"dev_id\":\"201602140001\"},{\"ord_id\":\"8089\",\"ord_info\":\"444444\",\"sn\":\"C20160400232\",\"dev_id\":\"201602140002\"},{\"ord_id\":\"3535\",\"ord_info\":\"3535\",\"sn\":\"C20160500001\",\"dev_id\":\"3535\"},{\"ord_id\":\"000222\",\"ord_info\":\"sn\",\"sn\":\"C20160500023\",\"dev_id\":\"201404290001\"}]}}";
JSONObject job = JSON.parseObject(str);
String locStr = job.get("loc").toString();
JSONObject locJson = JSON.parseObject(locStr);
String cargoStr = locJson.get("cargo").toString();
JSONArray cargoArr = JSON.parseArray(cargoStr);
for (int i = 0; i < cargoArr.size(); i++) {
System.out.println(cargoArr.get(i));

}

结果:

{"dev_id":"201404290001","ord_id":"S0003","ord_info":"箱子","sn":"C201602170003"}
{"dev_id":"201602140001","ord_id":"8089","ord_info":"444444","sn":"C20160400217"}
{"dev_id":"201602140002","ord_id":"8089","ord_info":"444444","sn":"C20160400232"}
{"dev_id":"3535","ord_id":"3535","ord_info":"3535","sn":"C20160500001"}
{"dev_id":"201404290001","ord_id":"000222","ord_info":"sn","sn":"C20160500023"}

用的是fastJson

0
Ryan-瑞恩
Ryan-瑞恩
JSONObject 内部可以自动完成。。你获取出来的时候,用JSONArray去接收。。。。
返回顶部
顶部