java后端把数据转换为树,map递归生成json树,返回给前端(后台转换)
作者:不等风雨,只等你
这篇文章主要介绍了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<Map<String,Object>> data = new ArrayList<>();
Map<String,Object> map = new HashMap<>();
map.put("id",1);
map.put("pid",0);
map.put("name","甘肃省");
data.add(map);
Map<String,Object> map2 = new HashMap<>();
map2.put("id",2);
map2.put("pid",1);
map2.put("name","天水市");
data.add(map2);
Map<String,Object> map3 = new HashMap<>();
map3.put("id",3);
map3.put("pid",2);
map3.put("name","秦州区");
data.add(map3);
Map<String,Object> map4 = new HashMap<>();
map4.put("id",4);
map4.put("pid",0);
map4.put("name","北京市");
data.add(map4);
Map<String,Object> 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树,返回给前端(后台转换)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
