Java实现JSON多层遍历

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java实现JSON多层遍历相关的知识,希望对你有一定的参考价值。

如以下的json串:
"result":["appobranchid":"1","appobranchname":"银州办事处","content":["appocnt":"5","appodate":"2019-01-07",
"appocnt":"5","appodate":"2019-01-08","appocnt":"5","appodate":"2019-01-09",
"appocnt":"5","appodate":"2019-01-10","appocnt":"5","appodate":"2019-01-11"],
"appobranchid":"2","appobranchname":"新城区办事处","content":["appocnt":"5","appodate":"2019-01-07",
"appocnt":"5","appodate":"2019-01-08","appocnt":"5","appodate":"2019-01-09","appocnt":"5","appodate":"2019-01-10",
"appocnt":"5","appodate":"2019-01-11"],
"appobranchid":"3","appobranchname":"开原办事处","content":["appocnt":"5","appodate":"2019-01-07","appocnt":"5","appodate":"2019-01-08",
"appocnt":"5","appodate":"2019-01-09","appocnt":"5","appodate":"2019-01-10","appocnt":"5","appodate":"2019-01-11"],
"appobranchid":"4","appobranchname":"昌图办事处","content":["appocnt":"5","appodate":"2019-01-07",
"appocnt":"5","appodate":"2019-01-08","appocnt":"5","appodate":"2019-01-09","appocnt":"5","appodate":"2019-01-10",
"appocnt":"5","appodate":"2019-01-11"],
"appobranchid":"5","appobranchname":"西丰办事处","content":["appocnt":"5","appodate":"2019-01-07","appocnt":"5","appodate":"2019-01-08",
"appocnt":"5","appodate":"2019-01-09","appocnt":"5","appodate":"2019-01-10","appocnt":"5","appodate":"2019-01-11"],
"appobranchid":"6","appobranchname":"调兵山办事处","content":["appocnt":"5","appodate":"2019-01-07",
"appocnt":"5","appodate":"2019-01-08","appocnt":"5","appodate":"2019-01-09","appocnt":"5","appodate":"2019-01-10",
"appocnt":"5","appodate":"2019-01-11"],
"appobranchid":"7","appobranchname":"清河办事处","content":["appocnt":"5","appodate":"2019-01-07","appocnt":"5","appodate":"2019-01-08",
"appocnt":"5","appodate":"2019-01-09","appocnt":"5","appodate":"2019-01-10","appocnt":"5","appodate":"2019-01-11"]]

想要实现的效果是
1 银州办事处 5 2019-01-07 5 2019-01-08 5 2019-01-09 5 2019-01-10 5 2019-01-11
2 新城办事处 5 2019-01-07 5 2019-01-08 5 2019-01-09 5 2019-01-10 5 2019-01-11
3 开原办事处 ....

重金悬赏,跪求大佬解析

参考技术A JSONObject jsonObject = new JSONObject(s);
然后用Iterator迭代器遍历取值,建议用反射机制解析到封装好的对象中
JSONObject jsonObject = new JSONObject(jsonString);
Iterator iterator = jsonObject.keys();while(iterator.hasNext())
key = (String) iterator.next();
value = jsonObject.getString(key);
参考技术B String json=“”
JSONObject obj = new JSONObject(json);
JSONArray result = obj.getJSONArray("result");

for(int i=0;i<result.size();i++)
String r = "";
JSONObject app = result.getJSONObject(i);
String appobranchid = app.getString("appobranchid");
String appobranchname = app.getString("appobranchname");
r = appobranchid +" "+ appobranchname;
JSONArray arr = app.getJSONArray("content");
for(int j=0;j<arr.size();j++)
JSONObject content = arr.getJSONObject(i);
String appcnt = content.getString("appocnt");
String appdate = content.getString("appodate");
r += appcnt +" " + appdate;

System.out.println(r);
本回答被提问者和网友采纳
参考技术C JSONObject jsonObject = new JSONObject(jsonString); Iterator iterator = jsonObject.keys(); while(iterator.hasNext()) key = (String) iterator.next(); value = jsonObject.getString(key); 参考技术D 好红红火火恍恍惚惚哈哈哈哈哈哈哈我都没有

以上是关于Java实现JSON多层遍历的主要内容,如果未能解决你的问题,请参考以下文章

java中怎么遍历jsonarray

java如何遍历json 请具体点

java json处理 怎么样遍历未知key 具体 看代码

java foreach是不是能对jsonarray进行遍历

java foreach是不是能对jsonarray进行遍历

java foreach是不是能对jsonarray进行遍历