我想解析一个树形的嵌套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的值应该怎么解析? 展开
 我来答
千古人文
2016-08-14 · TA获得超过388个赞
知道小有建树答主
回答量:233
采纳率:93%
帮助的人:87.5万
展开全部

一,你可以使用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"));
                        }
                    }
                }
            }
        }
    }
多儿美食记
2016-08-18 · TA获得超过147个赞
知道答主
回答量:53
采纳率:0%
帮助的人:34.9万
展开全部
使用递归方法解析,这是很常用的思路啊
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式