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 开原办事处 ....

重金悬赏,跪求大佬解析

第1个回答  2019-01-05
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);
}
第2个回答  2019-01-05
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);
}本回答被提问者和网友采纳
第3个回答  2019-01-05
JSONObject jsonObject = new JSONObject(jsonString); Iterator iterator = jsonObject.keys(); while(iterator.hasNext()){ key = (String) iterator.next(); value = jsonObject.getString(key); }
第4个回答  2019-01-05
好红红火火恍恍惚惚哈哈哈哈哈哈哈我都没有