zoukankan      html  css  js  c++  java
  • JAVA 转换 树结构数据

    JAVA 转换 树结构数据

    第一步:引入fastjson

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>${fastjson.version}</version>
    </dependency>
    

    第二步:用到了工具内的JSONPath

    JSONPath使用教程

        /**
         * 树转换
         *
         * @param obj                  需要转换的对象
         * @param parentCodeFieldName  父标识字段名
         * @param parentCode           父标识值
         * @param currentCodeFieldName 当前标识字段名
         * @param childrenFiledName    子树的字段名
         * @param c                    需要转换的Class类型
         * @param <T>                  泛型
         * @return 返回List<T>
         */
        public static <T> List<T> tree(Object obj, String parentCodeFieldName, String parentCode, String currentCodeFieldName, String childrenFiledName, Class<T> c) {
            long t1 = System.currentTimeMillis();
            String jsonStr = JSON.toJSONString(obj);
            log.debug("树转换开始 >>>>>>>>>>>>>>>> {}", JSON.toJSONString(obj));
            //获取第一层级的数据
            JSONArray jsonArray = (JSONArray) JSONPath.read(jsonStr, "$[" + parentCodeFieldName + "=" + parentCode + "]");
            if (CollectionUtils.isEmpty(jsonArray)) {
                //为空的话直接返回空集合
                return Lists.newArrayList();
            }
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String code = jsonObject.getString(currentCodeFieldName);
                treeChildren(jsonStr, jsonObject, parentCodeFieldName, code, currentCodeFieldName, childrenFiledName);
            }
            List<T> list = JSONArray.parseArray(jsonArray.toString(), c);
            log.debug("树转换结束, 转换时间: {} ms . >>>>>>>>>>>>>>>> {}", (System.currentTimeMillis() - t1), JSON.toJSONString(list));
            return list;
        }
    
        private static void treeChildren(String jsonStr, JSONObject currentJsonObj, String parentCodeFieldName, String parentCode, String currentCodeFieldName, String childrenFiledName) {
            JSONArray jsonArray = (JSONArray) JSONPath.read(jsonStr, "$[" + parentCodeFieldName + "=" + parentCode + "]");
            if (CollectionUtils.isEmpty(jsonArray)) {
                return;
            }
            currentJsonObj.put(childrenFiledName, jsonArray);
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String code = jsonObject.getString(currentCodeFieldName);
                treeChildren(jsonStr, jsonObject, parentCodeFieldName, code, currentCodeFieldName, childrenFiledName);
            }
        }
    

    赵小胖个人博客

  • 相关阅读:
    大名鼎鼎的红黑树,你get了么?2-3树 绝对平衡 右旋转 左旋转 颜色反转
    django 数据库连接模块解析及简单长连接改造
    django settings最佳配置
    Django 多数据库联用
    初步了解Shuttle ESB
    linux 线程切换效率与进程切换效率相差究竟有多大?
    进行mysql压力測试须要注意的几点
    POJ 2762 Going from u to v or from v to u?(强联通,拓扑排序)
    linux中O(1)调度算法与全然公平(CFS)调度算法
    LeetCode 121 Best Time to Buy and Sell Stock
  • 原文地址:https://www.cnblogs.com/Sky0914/p/12548368.html
Copyright © 2011-2022 走看看