list转json tree的工具类
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的工具类的更多相关文章
- JaxbUtil转json转XML工具类
json转换为XML工具类 package com.cxf.value; import org.springframework.util.StringUtils; import javax.xml.b ...
- 构造Json对象串工具类
import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.Property ...
- java 写一个JSON解析的工具类
上面是一个标准的json的响应内容截图,第一个红圈”per_page”是一个json对象,我们可以根据”per_page”来找到对应值是3,而第二个红圈“data”是一个JSON数组,而不是对象,不能 ...
- springmvc返回json数据的工具类
在ssm框架下,MVC向前端返回数据的json工具类代码如下: public class JsonResult<T> { public static final int SUCCESS=0 ...
- Json:Java对象和Json文本转换工具类
Json 是一个用于 Java 对象 和 Json 文本 相互转换的工具类. 安装 下载源码 git clone https://github.com/njdi/durian.git 编译源码 cd ...
- 自定义响应结构 Json格式转换 工具类
import java.util.List; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterx ...
- JSON参数解析工具类
/// <summary> /// 解析JSON参数 /// </summary> public class JSONParser { JObject jObj = null; ...
- List集合和JSON互转工具类
public class JsonListUtil { /** * List<T> 转 json 保存到数据库 */ public static <T> String list ...
- 【自制工具类】struts返回json数据包装格式类
自己写的一个给struts返回的json数据包装格式类,不喜勿喷,原创,需在项目中引入com.alibaba.fastjson的jar包 先看下效果(这里没有使用msg,有兴趣的往下看): 上demo ...
- Spring统一返回Json工具类,带分页信息
前言: 项目做前后端分离时,我们会经常提供Json数据给前端,如果有一个统一的Json格式返回工具类,那么将大大提高开发效率和减低沟通成本. 此Json响应工具类,支持带分页信息,支持泛型,支持Htt ...
随机推荐
- python结巴分词及词频统计
1 def get_words(txt): 2 seg_list = jieba.cut(txt) 3 c = Counter() 4 for x in seg_list: 5 if len(x) & ...
- MySQL创建和操纵表
表创建基础 CREATE TABLE customers ( cust_id int NOT NULL AUTO_INCREMENT , cust_name char(50) NOT NULL , c ...
- 如何保证MySQL和Redis数据一致性?
背景 在高并发的业务场景中,因为MySQL数据库是操作磁盘效率比较低,因此大多数情况下数据库都是高并发系统的瓶颈.因为Redis操作数据是在内存中进行,所以就需要使用Redis做一个缓存.让请求先访问 ...
- #树状数组#CF461C Appleman and a Sheet of Paper
题目传送门 分析 可以发现往左翻太多相当于往右翻一点,所以如果翻的位置超过一半那么打一个取反标记再另一边翻转, 用树状数组维护当前厚度,时间复杂度 \(O(n\log^2 n)\) 代码 #inclu ...
- OpenHarmony技术日圆满举行丨3.1 Release版本重磅发布,生态落地初具规模
4 月 25 日,"共建新技术,开拓新领域"OpenAtom OpenHarmony(以下简称"OpenHarmony")技术日在深圳顺利召开.活动现场,Ope ...
- Python 简介和用途
什么是Python? Python是一种流行的编程语言,由Guido van Rossum创建,并于1991年发布. 它用于以下领域: 网页开发(服务器端) 软件开发 数学 系统脚本编写 Python ...
- 重新点亮linux 命令树————网络管理和网络配置文件[十一六]
前言 简单整理一下网络管理和网络配置文件. 正文 网络服务程序分为两种:分别是SysV和systemd service network start|stop|restart chkconfig --l ...
- js 按照字母进行分组
前言 js 按照字母进行分组的一个实例. 正文 var list = [ { 'name' : '张三', 'py' : 'zhnagsan' }, { 'name' : '李四', 'py' : ' ...
- Docker compose 部署 nginx+php
Docker compose 部署 nginx+php 拉取Docker镜像 docker pull nginx:1.21.6 docker pull php:7.4.28-fpm 创建docker- ...
- Pytorch-卷积神经网络CNN之lenet5的Pytorch代码实现
先说一个小知识,助于理解代码中各个层之间维度是怎么变换的. 卷积函数:一般只用来改变输入数据的维度,例如3维到16维. Conv2d() Conv2d(in_channels:int,out_chan ...