栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java后端把数据转换为树,map递归生成json树,返回给前端(后台转换)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java后端把数据转换为树,map递归生成json树,返回给前端(后台转换)

java 后端,把数据转换为树,map递归生成一颗json树,返回给前端(后台转换)

1.为什么要写这样的一篇博客?

2.java 后端代码

3. 转化完的数据在前端格式为类似于:

1.为什么要写这样的一篇博客?

在公司的实习的时候,遇到了一个略坑的东西,就是要医院的科室通过其子父id做成一颗项目树,但是科室的层次有很多,有点甚至到了六层,导致最终选择了优化后的递归算法。

如果在三层或者三层以下,可以考虑使用内部类,超过三层的话,最好就使用递归了,不过记得必须的优化。

2.java 后端代码

代码的解释和理解我卸载代码里面,返回到前端会自动转换成Json格式的数据。

//第一个参数,需要生成树的数组,第二个参数为树的根节点
public JSonObject getJsontree(JSonArray json,JSonObject job){
 JSonArray tempJson = JSONArray.fromObject("[]");
 //筛选出父id等于job里面id的科室
 for(int i = 0;i < json.size();i++) 
 { 
 //这里可以使用Iterator 
 if(json.getJSonObject(i).get("parent_id").equals(job.get("unit_sn"))) {  
  tempJson.add(json.getJSonObject(i));
 }; 
 }
 // 优化,减少科室集合的数量,避免重复查询,有再优化的方法,希望告知。。
 json.removeAll(tempJson);
 for(int i = 0;i < tempJson.size(); i ++) {
 //对第二层进行递归,此处类推
 getJsontree(json, tempJson.getJSonObject(i));
 }
 //生成完的树结构map集合加到根节点
 if(tempJson.size()!=0)
 job.put("children", tempJson);
 return job; 
}

3. 转化完的数据在前端格式为类似于:

[
 { text: '节点1', children: [
  { text: '节点1.1' },
  { text: '节点1.2' },
  { text: '节点1.3', children: [
  { text: '节点1.3.1' },
  { text: '节点1.3.2' }
  ]
  },
  { text: '节点1.4' }
 ]
 }
 ]

补充知识:java将list转为树形结构的方法

1、通过转化成json封装数据

[
 {
 "name":"甘肃省",
 "pid":0,
 "id":1
 },
 {
 "name":"天水市",
 "pid":1,
 "id":2
 },
 {
 "name":"秦州区",
 "pid":2,
 "id":3
 },
 {
 "name":"北京市",
 "pid":0,
 "id":4
 },
 {
 "name":"昌平区",
 "pid":4,
 "id":5
 }
]

现需要是使用java将以上数据转为树形结构,转化后下的结构如下

[
 {
 "children":[
  {
  "children":[
   {
   "name":"秦州区",
   "pid":2,
   "id":3
   }
  ],
  "name":"天水市",
  "pid":1,
  "id":2
  }
 ],
 "name":"甘肃省",
 "pid":0,
 "id":1
 },
 {
 "children":[
  {
  "name":"昌平区",
  "pid":4,
  "id":5
  }
 ],
 "name":"北京市",
 "pid":0,
 "id":4
 }
]

代码如下

 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 
 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的值
  String pidStr = "";
  Object pidObj = aVal.get(pid);
  if (aVal.get(pid) != null) {
  pidStr = aVal.get(pid).toString();
  }
  JSonObject hashVP = (JSONObject) hash.get(pidStr);
  //如果记录的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){
 List> data = new ArrayList<>();
 Map map = new HashMap<>();
 map.put("id",1);
 map.put("pid",0);
 map.put("name","甘肃省");
 data.add(map);
 Map map2 = new HashMap<>();
 map2.put("id",2);
 map2.put("pid",1);
 map2.put("name","天水市");
 data.add(map2);
 Map map3 = new HashMap<>();
 map3.put("id",3);
 map3.put("pid",2);
 map3.put("name","秦州区");
 data.add(map3);
 Map map4 = new HashMap<>();
 map4.put("id",4);
 map4.put("pid",0);
 map4.put("name","北京市");
 data.add(map4);
 Map map5 = new HashMap<>();
 map5.put("id",5);
 map5.put("pid",4);
 map5.put("name","昌平区");
 data.add(map5);
 System.out.println(JSON.toJSonString(data));
 JSonArray result = 
 listToTree(JSONArray.parseArray(JSON.toJSonString(data)),"id","pid","children");
 System.out.println(JSON.toJSonString(result));
}

以上这篇java后端把数据转换为树,map递归生成json树,返回给前端(后台转换)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/148139.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号