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"
}
]

方法拓展

 public List<DictTypeVO> getTree() {
List<DictTypeVO> dictTypeVOS = DictTypeDomainService().findAllList();
return buildTreeUseList(dictTypeVOS, 0);
} /**
* 使用递归构建树结构-使用foreach转换
* @param treeList
* @param id
* @return
*/
public static List<DictTypeVO> buildTreeUseList(List<DictTypeVO> treeList,long id ) {
//收集传递的集合中父id相同的TreeNode
List<DictTypeVO> children = new ArrayList<>();
for (DictTypeVO treeNode : treeList) {
//判断该节点的父id,是否与传入的父id相同,相同则递归设置其孩子节点,并将该节点放入children集合中
if(treeNode.getParentId() == id){
//递归设置其孩子节点
treeNode.setChildren(buildTreeUseList(treeList, treeNode.getId()));
//放入children集合
children.add(treeNode);
}
}
return children;
}

list转json tree的工具类的更多相关文章

  1. JaxbUtil转json转XML工具类

    json转换为XML工具类 package com.cxf.value; import org.springframework.util.StringUtils; import javax.xml.b ...

  2. 构造Json对象串工具类

    import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.Property ...

  3. java 写一个JSON解析的工具类

    上面是一个标准的json的响应内容截图,第一个红圈”per_page”是一个json对象,我们可以根据”per_page”来找到对应值是3,而第二个红圈“data”是一个JSON数组,而不是对象,不能 ...

  4. springmvc返回json数据的工具类

    在ssm框架下,MVC向前端返回数据的json工具类代码如下: public class JsonResult<T> { public static final int SUCCESS=0 ...

  5. Json:Java对象和Json文本转换工具类

    Json 是一个用于 Java 对象 和 Json 文本 相互转换的工具类. 安装 下载源码 git clone https://github.com/njdi/durian.git 编译源码 cd ...

  6. 自定义响应结构 Json格式转换 工具类

    import java.util.List; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterx ...

  7. JSON参数解析工具类

    /// <summary> /// 解析JSON参数 /// </summary> public class JSONParser { JObject jObj = null; ...

  8. List集合和JSON互转工具类

    public class JsonListUtil { /** * List<T> 转 json 保存到数据库 */ public static <T> String list ...

  9. 【自制工具类】struts返回json数据包装格式类

    自己写的一个给struts返回的json数据包装格式类,不喜勿喷,原创,需在项目中引入com.alibaba.fastjson的jar包 先看下效果(这里没有使用msg,有兴趣的往下看): 上demo ...

  10. Spring统一返回Json工具类,带分页信息

    前言: 项目做前后端分离时,我们会经常提供Json数据给前端,如果有一个统一的Json格式返回工具类,那么将大大提高开发效率和减低沟通成本. 此Json响应工具类,支持带分页信息,支持泛型,支持Htt ...

随机推荐

  1. Python 集合(Sets)3

    Python - 合并集合 在 Python 中,有几种方法可以合并两个或多个集合.您可以使用union()方法,该方法返回一个包含两个集合中所有项的新集合,或使用update()方法,将一个集合中的 ...

  2. 基于EtherNet/IP实现欧姆龙NX系列PLC通信

    1.引言 工业以太网协议 (Ethernet/IP) 是由ODVA所开发并得到了罗克韦尔自动化的强大支持.它使用已用于ControlNet和DeviceNet的控制和信息协议 (CIP) 为应用层协议 ...

  3. (数据科学学习手札159)使用ruff对Python代码进行自动美化

    本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 大家好我是费老师,在日常编写Python代码的过 ...

  4. 重学c#系列——索引器(十)

    前言 对于索引器,我们肯定不会陌生.为什么会有索引器这个东西呢?我们发现这东西很像是属性,只是通过索引方式去调用,那就慢慢揭开他的面纱吧. 正文 假设我们要对外暴露一个数组,为了体现其封装性,我们使用 ...

  5. Deep Learning on Graphs: A Survey第五章自动编码论文总结

    论文地址:https://arxiv.org/pdf/1812.04202.pdf 最近老师让我们读的一片论文,已经开组会讲完了,我负责的是第五章,图的自动编码,现在再总结一遍,便于后者研读.因为这篇 ...

  6. WPF随笔收录-实时绘制心率曲线

    一.前言 在自己的项目中,涉及到实时心率曲线的绘制,项目上的曲线绘制,一般很难找到能直接用的第三方库,而且有些还是定制化的功能,所以还是自己绘制比较方便.很多人一听到自己画就害怕,感觉很难,今天就分享 ...

  7. locust的 -T,--tags使用

    官网的TAG配置说明:-T [TAG [TAG ...]], --tags [TAG [TAG ...]]List of tags to include in the test, so only ta ...

  8. Django框架——csrf跨站请求伪造、csrf校验、csrf相关装饰器、auth认证、auth认证相关模块及操作

    csrf跨站请求伪造 钓鱼网站:模仿一个正规的网站 让用户在该网站上做操作 但操作的结果会影响到用户正常的网站账户 但是其中有一些猫腻 eg:英语四六级考试需要网上先缴费 但是你会发现卡里的钱扣了但是 ...

  9. 顺通ERP:精细敏捷的设计理念,得到了消费者的喜爱

    顺通ERP是近年来备受关注的一款ERP品牌,其设计精细,操作便捷,备受消费者喜爱.那么,顺通ERP到底怎么样呢?属于什么档次呢? 首先,从品质上来看,顺通ERP具备高度的稳定性,能够确保企业的日常运营 ...

  10. 科学地花钱:基于端智能的在线红包分配方案 (CIKM2020)

    简介: 红包是电商平台重要的用户运营手段,本文将介绍1688基于端智能技术开发的two-stage红包分发方案.这一方案持续在线上生效,相较于原有算法有明显提升. 一.前言 本文是作者在1688进行新 ...