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;
    }
  • 相关阅读:
    CRM 线索 客户 统称为 资源 客户服务管理篇 销售易
    IM 简介
    蚂蚁区块链
    区块链存证
    TQ2440 LCD试验失败经验教训
    调色板的概念
    分离链接散列表C语言实现实例
    Nor Flash启动和Nand Flash启动时Stepping stone都在哪?
    ADS中编译现存项目时常见错误:无法打开文件……2440init.o的解决办法
    TQ2440之定时器中断0——volatile关键字的重要作用
  • 原文地址:https://www.cnblogs.com/ViokingJava/p/10267723.html
Copyright © 2011-2022 走看看