java后端把数据转换为树,map递归生成json树,返回给前端(后台转换)
java后端,把数据转换为树,map递归生成一颗json树,返回给前端(后台转换)
1.为什么要写这样的一篇博客?
2.java后端代码
3.转化完的数据在前端格式为类似于:
1.为什么要写这样的一篇博客?
在公司的实习的时候,遇到了一个略坑的东西,就是要医院的科室通过其子父id做成一颗项目树,但是科室的层次有很多,有点甚至到了六层,导致最终选择了优化后的递归算法。
如果在三层或者三层以下,可以考虑使用内部类,超过三层的话,最好就使用递归了,不过记得必须的优化。
2.java后端代码
代码的解释和理解我卸载代码里面,返回到前端会自动转换成Json格式的数据。
//第一个参数,需要生成树的数组,第二个参数为树的根节点
publicJSONObjectgetJsontree(JSONArrayjson,JSONObjectjob){
JSONArraytempJson=JSONArray.fromObject("[]");
//筛选出父id等于job里面id的科室
for(inti=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(inti=0;i<tempJson.size();i++){
//对第二层进行递归,此处类推
getJsontree(json,tempJson.getJSONObject(i));
}
//生成完的树结构map集合加到根节点
if(tempJson.size()!=0)
job.put("children",tempJson);
returnjob;
}
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
}
]
代码如下
importcom.alibaba.fastjson.JSON;
importcom.alibaba.fastjson.JSONArray;
importcom.alibaba.fastjson.JSONObject;
publicstaticJSONArraylistToTree(JSONArrayarr,Stringid,Stringpid,Stringchild){
JSONArrayr=newJSONArray();
JSONObjecthash=newJSONObject();
//将数组转为Object的形式,key为数组中的id
for(inti=0;i<arr.size();i++){
JSONObjectjson=(JSONObject)arr.get(i);
hash.put(json.getString(id),json);
}
//遍历结果集
for(intj=0;j<arr.size();j++){
//单条记录
JSONObjectaVal=(JSONObject)arr.get(j);
//在hash中取出key为单条记录中pid的值
StringpidStr="";
ObjectpidObj=aVal.get(pid);
if(aVal.get(pid)!=null){
pidStr=aVal.get(pid).toString();
}
JSONObjecthashVP=(JSONObject)hash.get(pidStr);
//如果记录的pid存在,则说明它有父节点,将她添加到孩子节点的集合中
if(hashVP!=null){
//检查是否有child属性
if(hashVP.get(child)!=null){
JSONArraych=(JSONArray)hashVP.get(child);
ch.add(aVal);
hashVP.put(child,ch);
}else{
JSONArraych=newJSONArray();
ch.add(aVal);
hashVP.put(child,ch);
}
}else{
r.add(aVal);
}
}
returnr;
}
publicstaticvoidmain(String[]args){
List<Map<String,Object>>data=newArrayList<>();
Map<String,Object>map=newHashMap<>();
map.put("id",1);
map.put("pid",0);
map.put("name","甘肃省");
data.add(map);
Map<String,Object>map2=newHashMap<>();
map2.put("id",2);
map2.put("pid",1);
map2.put("name","天水市");
data.add(map2);
Map<String,Object>map3=newHashMap<>();
map3.put("id",3);
map3.put("pid",2);
map3.put("name","秦州区");
data.add(map3);
Map<String,Object>map4=newHashMap<>();
map4.put("id",4);
map4.put("pid",0);
map4.put("name","北京市");
data.add(map4);
Map<String,Object>map5=newHashMap<>();
map5.put("id",5);
map5.put("pid",4);
map5.put("name","昌平区");
data.add(map5);
System.out.println(JSON.toJSONString(data));
JSONArrayresult=
listToTree(JSONArray.parseArray(JSON.toJSONString(data)),"id","pid","children");
System.out.println(JSON.toJSONString(result));
}
以上这篇java后端把数据转换为树,map递归生成json树,返回给前端(后台转换)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。