zoukankan      html  css  js  c++  java
  • Java 格式化json为json树

    /**
         * @param treeList      数据
         * @param parentIdValue 父级值
         * @param parentIdFiled 父级字段名称
         * @param idFiled       子级字段名称
         * @Description json转jsonTree
         * @Author dongjd
         * @Date 19-1-11 下午5:38
         * @Return com.alibaba.fastjson.JSONArray
         */
        public static JSONArray jsonTreeFormat(List<Map<String, Object>> treeList, String parentIdValue, String parentIdFiled, String idFiled) {
            JSONArray childMenu = new JSONArray();
            treeList.stream().map(map -> JSONObject.parseObject(JSON.toJSONString(map))).forEach(jsonMenu -> {
                String menuId = jsonMenu.getString(idFiled);
                String pid = jsonMenu.getString(parentIdFiled);
                if (parentIdValue.equals(pid)) {
                    JSONArray childList = jsonTreeFormat(treeList, menuId, parentIdFiled, idFiled);
                    jsonMenu.put("childList", childList);
                    childMenu.add(jsonMenu);
                }
            });
            return childMenu;
        }

    参考资料[1]:https://blog.csdn.net/jianxia801/article/details/82771346

    /**
    * @param treeList
    * @param parentIdValue 级值
    * @param parentIdFiled 字段名
    * @param idFiled 字段名
    * @Description jsonjsonTree
    * @Author dongjd
    * @Date 19-1-11 下午5:38
    * @Return com.alibaba.fastjson.JSONArray
    */
    public static JSONArray jsonTreeFormat(List<Map<String, Object>> treeList, String parentIdValue, String parentIdFiled, String idFiled) {
    JSONArray childMenu = new JSONArray();
    treeList.stream().map(map -> JSONObject.parseObject(JSON.toJSONString(map))).forEach(jsonMenu -> {
    String menuId = jsonMenu.getString(idFiled);
    String pid = jsonMenu.getString(parentIdFiled);
    if (parentIdValue.equals(pid)) {
    JSONArray childList = jsonTreeFormat(treeList, menuId, parentIdFiled, idFiled);
    jsonMenu.put("childList", childList);
    childMenu.add(jsonMenu);
    }
    });
    return childMenu;
    }
  • 相关阅读:
    你喜欢使用eclipse+tomcat编程吗?!
    "ERR_GFX_D3D_INIT", GTA5-报错解决办法
    这样写JS的方式对吗?
    webui layout like desktop rich client
    2014年12月23日00:42:54——PS4
    2014年12月20日00:33:14-遮罩+进度条-extjs form.isvalid
    十分钟部署智能合约
    idea clion编译器
    parity 注记词
    go语言学习笔记
  • 原文地址:https://www.cnblogs.com/ViokingJava/p/10267723.html
Copyright © 2011-2022 走看看