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;
    }
  • 相关阅读:
    ssh免密登录
    idea搭建flink环境
    idea快捷键列表
    scala对复杂json的处理
    配置三台服务器的时间同步-linux 及 ntp.conf配置文件详解
    91. Reverse Linked List 反转链表
    92. Reverse Linked List II 反转链表 II
    121. Best Time to Buy and Sell Stock 买卖股票的最佳时机
    53. Maximum Subarray最大子序和
    70. Climbing Stairs爬楼梯
  • 原文地址:https://www.cnblogs.com/ViokingJava/p/10267723.html
Copyright © 2011-2022 走看看