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 ...
随机推荐
- #树形dp#洛谷 4395 [BOI2003]Gem 气垫车
题目 给出一棵树,要求你为树上的结点标上权值,权值可以是任意的正整数 唯一的限制条件是相邻的两个结点不能标上相同的权值,要求一种方案,使得整棵树的总价值最小. 分析 每个结点的权值最大可能为 \(\l ...
- #高精度,排列组合、dp#JZOJ 2755 树的计数
题目 求\(n\)个点直径为\(d\)的标号树个数(多组数据) (\(0\leq d\leq n\leq 50,n>0\)) 分析 首先特判一下\(n==d\)无解,\(d=0\)除非只有一个点 ...
- Bootstrap实战 - 单页面网站
一.介绍 单页面结构简单.布局清晰,常常用来做手机 App 或者某个产品的下载介绍页面.现在,展示型网页整体趋向于单页网站设计,这样一次性把核心信息展现出来,对于用户来说更加直观和简单,能够快速了解一 ...
- 【福利活动】深度体验OpenHarmony对接华为云IoT
本文主要介绍基于OpenHarmony 3.0来接入IoTDA,以BearPi-HM_Nano开发板为例,使用huaweicloud_iot_link SDK对接华为云物联网平台的简单流程.文末为 ...
- 掌上新闻随心播控,HarmonyOS SDK助力新浪新闻打造精致易用的资讯服务新体验
原生智能是HarmonyOS NEXT的核心亮点之一,依托HarmonyOS SDK丰富全面的开放能力,开发者只需通过几行代码,即可快速实现AI功能.新浪新闻作为鸿蒙原生应用开发的先行者之一,从有声资 ...
- win7系统部署django项目
win7系统部署django项目,在Windows电脑上我使用的是 Apache + mod_wsgi 来部署的 # 背景 Python 3.8.3 Django 3.1.7 Apach ...
- c# 框架系列 ———— EFCore 模型篇 [一]
前言 简单介绍一下EfCore 的模型篇 正文 内容来源: 配置模型 配置模型的方式,一种是fluent api 还一种是属性的方式. public class Blog { public int B ...
- mysql 必知必会整理—触发器[十五]
前言 现在很多都是程序用于触发的,而不是触发器了. 正文 需要MySQL 5 对触发器的支持是在MySQL 5中增加的.因此,本章内容适用于MySQL 5或之后的版本. MySQL语句在需要时被执行, ...
- 接口API用例自动转locust测试用例
做接口测试是必要的,写完接口测试用例,再写locust压测脚本,其实差异不大: 写个简单的py,把接口测试脚本转为locust压测脚本,本例只是简单的示范: 原接口校验脚本: 1 # -*- codi ...
- 微信小程序,本地和真机测试都是好的,但体验版扫码显示空白页
大概率是缓存导致的,删除掉你手机上的小程序[开发版] 和 [体验版],然后再扫码进入体验版就好了.