首页 > 其他分享 >list转json tree的工具类

list转json tree的工具类

时间:2022-12-30 13:22:24浏览次数:40  
标签:name JSONArray tree list param json parentId import id

package com.glodon.safety.contingency.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.*;

/**
 * @Description 通用的list转json tree的工具类
 * @Author songwp
 * @Date 2022/12/30 12:59
 * @Version 1.0.0
 **/
public class CommonTreeUtil {
    /**
     - listToTree
     - <p>方法说明<p>
     - 将JSONArray数组转为树状结构
     - @param arr 需要转化的数据
     - @param id 数据唯一的标识键值
     - @param pid 父id唯一标识键值
     - @param child 子节点键值
     - @return JSONArray
     */
    public static JSONArray listToTree(JSONArray arr, String id, String pid, String child){
        JSONArray r = new JSONArray();
        JSONObject hash = new JSONObject();
        //将数组转为Object的形式,key为数组中的id
        for(int i=0;i<arr.size();i++){
            JSONObject json = (JSONObject) arr.get(i);
            hash.put(json.getString(id), json);
        }
        //遍历结果集
        for(int j=0;j<arr.size();j++){
            //单条记录
            JSONObject aVal = (JSONObject) arr.get(j);
            //在hash中取出key为单条记录中pid的值
            JSONObject hashVP = (JSONObject) hash.get(aVal.get(pid).toString());
            //如果记录的pid存在,则说明它有父节点,将她添加到孩子节点的集合中
            if(hashVP!=null){
                //检查是否有child属性
                if(hashVP.get(child)!=null){
                    JSONArray ch = (JSONArray) hashVP.get(child);
                    ch.add(aVal);
                    hashVP.put(child, ch);
                }else{
                    JSONArray ch = new JSONArray();
                    ch.add(aVal);
                    hashVP.put(child, ch);
                }
            }else{
                r.add(aVal);
            }
        }
        return r;
    }

    public static void main(String[] args) {
        Map map = new HashMap();
        map.put("id","1");
        map.put("parentId","0");
        map.put("name","陕西省");
        Map map1 = new HashMap();
        map1.put("id","2");
        map1.put("parentId","1");
        map1.put("name","西安市");
        Map map2 = new HashMap();
        map2.put("id","3");
        map2.put("parentId","2");
        map2.put("name","雁塔区");
        Map map3 = new HashMap();
        map3.put("id","4");
        map3.put("parentId","1");
        map3.put("name","咸阳市");
        List<Map> mapList = Arrays.asList(map, map1, map2, map3);
        JSONArray result = CommonTreeUtil.listToTree(JSONArray.parseArray(JSON.toJSONString(mapList)),"id","parentId","children");
        System.out.println(result);

    }
}

控制台输出:

[
    {
        "children":[
            {
                "children":[
                    {
                        "name":"雁塔区",
                        "id":"3",
                        "parentId":"2"
                    }
                ],
                "name":"西安市",
                "id":"2",
                "parentId":"1"
            },
            {
                "name":"咸阳市",
                "id":"4",
                "parentId":"1"
            }
        ],
        "name":"陕西省",
        "id":"1",
        "parentId":"0"
    }
]

标签:name,JSONArray,tree,list,param,json,parentId,import,id
From: https://www.cnblogs.com/songweipeng/p/17014687.html

相关文章

  • 【CF1481F】AB Tree(单调队列优化多重背包)
    容易看出答案下界是树的最大深度,且构造方法只能是每一层的节点都染成同种颜色,可行性的判断是个背包问题。然后发现若不可行,就把除最后一层外的其它层每层都染成同种颜色,然......
  • MySQL中B-Tree和B+Tree创建过程
    1.B-Tree以一颗最大度数为5(5阶)的B-tree为例,每个节点最多存储4个key,5个指针。意味着:在一个有n个key的节点中,有n+1个指针,原理如下图:现在,依次存入如下数据:200、100、400、......
  • JSON学习
    1.JSON语法是JavaScript对象表示语法的子集。l 数据在名称/值对中l 数据由逗号分隔l 花括号保存对象l 方括号保存数组JSON 值可以是:l 数字(整......
  • ngui的betterlist做了哪些性能改进?
    betterlist简介:  betterlist是对System.Collections.Generic.List的改进。  betterlist版本对应该连接:https://github.com/NewbieGameCoder/BetterList  作者的目......
  • Potree 001 Potree介绍
    1、Potree是什么Potree是一种基于WebGL的点云数据可视化解决方案,包含点云数据转化,以及进行可视化的源码。该解决方案的主要优势在于对点云数据进行了多尺度的管理,在数据传......
  • tree reconstruct 834
    834. SumofDistancesinTreeHardThereisanundirectedconnectedtreewith n nodeslabeledfrom 0 to n-1 and n-1 edges.Youaregiventhe......
  • window.onerror 和window.addEventListener('error')的区别
    1.定义window.onerror全局事件函数window.onerror=function(message,source,lineno,colno,error){...}/**message:错误信息(字符串)。可用于HTMLonerror="......
  • java 列表迭代器 listIterator
    listIterator:            listIterator和iterator是有区别的,listIterator不会校验实际修改值和预期修改值是否相等,会把实际修改值赋值......
  • Spring源码:ConfigFileApplicationListener
    /**Copyright2012-2017theoriginalauthororauthors.**LicensedundertheApacheLicense,Version2.0(the"License");*youmaynotusethisfileexcept......
  • HashMap 和 treemap
    Map接口概述将键映射到值的对象一个映射不能包含重复的键每个键最多只能映射到一个值Map接口和Collection接口的不同Map是双列的,Col......