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. IntelliJ IDEA集成本地Maven步骤

    IntelliJ IDEA集成本地Maven步骤 一.前期准备 Maven已经在本地环境配置完成,步骤可以参考我的这篇文章: https://www.cnblogs.com/rainbow-1/p/1 ...

  2. ET介绍——浅谈AI框架

    AI框架 1. 几种AI的设计 AI在游戏中很多,但是为什么大家总是感觉ai编写起来十分困难,我后来思考了一番,主要原因是使用的方法不当.之前大家编写ai主要有几种方案: a. 状态机 我是不知道谁想 ...

  3. 使用site-maven-plugin在github上搭建公有仓库

    目录 简介 前期准备 在maven中配置GitHub权限 配置deploy-plugin 配置site-maven-plugin 怎么使用这个共享的项目 总结 简介 Maven是我们在开发java程序 ...

  4. OpenHarmony Meetup常州站招募令

    OpenHarmony Meetup 常州站正火热招募中! 诚邀充满激情的开发者参与线下盛会~ 探索OpenHarmony前沿科技,畅谈未来前景, 感受OpenHarmony生态构建之路的魅力! 线下 ...

  5. OpenHarmony源码解析之电话子系统——通话流程

    (以下内容来自开发者分享,不代表 OpenHarmony 项目群工作委员会观点) 王大鹏 深圳开鸿数字产业发展有限公司 一.简介 OpenAtom OpenHarmony(以下简称"Open ...

  6. Makefile 常用命令详解

    在软件开发中,Makefile是一种非常常用的自动化工具.Makefile文件包含了一系列规则,用于编译.打包.测试等操作,可以帮助我们自动化这些操作,提高项目的管理和编译效率.本文将介绍Makefi ...

  7. DEB打包教程

    一.deb简介 deb是一种安装包的格式,linux上常见的安装包主要是deb.rpm 二.deb简单使用 # deb安装 sudo dpkg -i webcamera_1.0_amd64.deb # ...

  8. SpringBoot+阿里云OCR图片识别

    准备条件:阿里云OCR图片识别API购买,初次购买1分钱500次接口调用 一.控制层 @GetMapping("/uploadManual") @ApiOperation(&quo ...

  9. c# aspose操作word文档

    背景 这个是一个操作word文档的插件 1.1插入图片 using Aspose.Words; using Aspose.Words.Drawing; using Aspose.Words.Rende ...

  10. TypeScript 中泛型的理解?应用场景?

    一.是什么 泛型程序设计(generic programming)是程序设计语言的一种风格或范式 泛型允许我们在强类型程序设计语言中编写代码时使用一些以后才指定的类型,在实例化时作为参数指明这些类型 ...