我想解析一个树形的嵌套json应该怎么解析它?
我想解析一个树形的嵌套json应该怎么解析它?json格式大致是这样{"StatusCode":"10000","result":[{"categoryid":1,"ca...
我想解析一个树形的嵌套json应该怎么解析它?json格式大致是这样{"StatusCode":"10000","result":[{"categoryid":1,"categorycode":"001","categoryname":"生产","parentid":0,"isleaf":0,"levelcode":"001","Description":"","state":1,"children":[{"categoryid":4,"categorycode":"001001","categoryname":"锅炉","parentid":1,"isleaf":0,"levelcode":"001001","Description":"","state":1,"children":[{"categoryid":6,"categorycode":"001001001","categoryname":"锅炉运行","parentid":4,"isleaf":1,"levelcode":"001001001","Description":"","state":1,"children":""},{"categoryid":7,"categorycode":"001001002","categoryname":"锅炉检修","parentid":4,"isleaf":1,"levelcode":"001001002","Description":"","state":1,"children":""}]},{"categoryid":5,"categorycode":"001002","categoryname":"汽机","parentid":1,"isleaf":1,"levelcode":"001002","Description":"","state":1,"children":""}]},{"categoryid":2,"categorycode":"002","categoryname":"管理","parentid":0,"isleaf":0,"levelcode":"002","Description":"","state":1,"children":[{"categoryid":8,"categorycode":"002001","categoryname":"人资","parentid":2,"isleaf":1,"levelcode":"002001","Description":"","state":1,"children":""}]},{"categoryid":3,"categorycode":"003","categoryname":"经营","parentid":0,"isleaf":1,"levelcode":"003","Description":"","state":1,"children":""}]}我想要得到所有categoryname的值应该怎么解析?
展开
2个回答
展开全部
一,你可以使用fastjson来解析
二,下面是一个例子,你再整理一下。顺便给你推荐一个网站:http://www.youxijishu.com
public static void main(String[] args) {
String str = "{'StatusCode':'10000','result':[{'categoryid':1,'categorycode':'001','categoryname':'生产','parentid':0,'isleaf':0,'levelcode':'001','Description':'','state':1,'children':[{'categoryid':4,'categorycode':'001001','categoryname':'锅炉','parentid':1,'isleaf':0,'levelcode':'001001','Description':'','state':1,'children':[{'categoryid':6,'categorycode':'001001001','categoryname':'锅炉运行','parentid':4,'isleaf':1,'levelcode':'001001001','Description':'','state':1,'children':''},{'categoryid':7,'categorycode':'001001002','categoryname':'锅炉检修','parentid':4,'isleaf':1,'levelcode':'001001002','Description':'','state':1,'children':''}]},{'categoryid':5,'categorycode':'001002','categoryname':'汽机','parentid':1,'isleaf':1,'levelcode':'001002','Description':'','state':1,'children':''}]},{'categoryid':2,'categorycode':'002','categoryname':'管理','parentid':0,'isleaf':0,'levelcode':'002','Description':'','state':1,'children':[{'categoryid':8,'categorycode':'002001','categoryname':'人资','parentid':2,'isleaf':1,'levelcode':'002001','Description':'','state':1,'children':''}]},{'categoryid':3,'categorycode':'003','categoryname':'经营','parentid':0,'isleaf':1,'levelcode':'003','Description':'','state':1,'children':''}]}";
JSONObject json = JSON.parseObject(str);
JSONArray jsonArrResult = (JSONArray) json.get("result");
if (jsonArrResult != null && !jsonArrResult.isEmpty()) {
int size = jsonArrResult.size();
for (int i = 0; i < size; i++) {
JSONObject obj1 = jsonArrResult.getJSONObject(i);
System.out.println(obj1.getString("categoryname"));
Object childrenObj = obj1.get("children");
if (childrenObj instanceof JSONArray) {
JSONArray jsonArrchildren = (JSONArray) childrenObj;
if (jsonArrchildren != null && !jsonArrchildren.isEmpty()) {
int childrenSize = jsonArrchildren.size();
for (int j = 0; j < childrenSize; j++) {
JSONObject obj2 = jsonArrchildren.getJSONObject(j);
System.out.println(obj2.get("categoryname"));
}
}
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询