The back end of java converts the data into a tree and map recursively generates the json tree which is returned to the front end of of for background conversion

  • 2020-05-27 05:45:35
  • OfStack

The java back end converts the data into a tree, and map recursively generates an json tree and returns it to the front end (background conversion).

1. Why write a blog like this?

2.java back-end code

3. The converted data in the front-end format is similar to:

1. Why write a blog like this?

During my internship in the company, I came across a slightly boring thing, that is, I asked the department of the hospital to make a project tree through its son and father id. However, there were many levels of departments, some of which even reached 6 levels, leading to the final choice of the optimized recursive algorithm.

Consider using inner classes if you're at or below level 3, and recursion if you're above level 3, but remember the necessary optimizations.

2.java back-end code

Code interpretation and understanding I uninstall the code inside, back to the front end will automatically be converted to Json format data.


// The first 1 The parameters need to generate an array of the tree, the first 2 Are the root nodes of the tree 
public JSONObject getJsontree(JSONArray json,JSONObject job){
 JSONArray tempJson = JSONArray.fromObject("[]");
 // Select the parent id Is equal to the job inside id The department of 
 for(int i = 0;i < json.size();i++) 
 { 
 // You can use it here Iterator 
 if(json.getJSONObject(i).get("parent_id").equals(job.get("unit_sn"))) {  
  tempJson.add(json.getJSONObject(i));
 }; 
 }
 //  Optimization,, reduce the number of departments set, to avoid repeated queries, there are re-optimization methods, I hope to inform. 
 json.removeAll(tempJson);
 for(int i = 0;i < tempJson.size(); i ++) {
 // For the first 2 Layers recurse, and so on 
 getJsontree(json, tempJson.getJSONObject(i));
 }
 // Generate the finished tree structure map The collection is added to the root node 
 if(tempJson.size()!=0)
 job.put("children", tempJson);
 return job; 
}

3. The converted data in the front-end format is similar to:


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

Additional knowledge: java's method for converting list into a tree

1. Encapsulate data by converting it to json


[
 {
 "name":" Gansu province ",
 "pid":0,
 "id":1
 },
 {
 "name":" tianshui ",
 "pid":1,
 "id":2
 },
 {
 "name":" The qin state ",
 "pid":2,
 "id":3
 },
 {
 "name":" The Beijing municipal ",
 "pid":0,
 "id":4
 },
 {
 "name":" Changping district ",
 "pid":4,
 "id":5
 }
]

Now we need to use java to convert the above data into a tree structure, and the structure below is as follows


[
 {
 "children":[
  {
  "children":[
   {
   "name":" The qin state ",
   "pid":2,
   "id":3
   }
  ],
  "name":" tianshui ",
  "pid":1,
  "id":2
  }
 ],
 "name":" Gansu province ",
 "pid":0,
 "id":1
 },
 {
 "children":[
  {
  "name":" Changping district ",
  "pid":4,
  "id":5
  }
 ],
 "name":" The Beijing municipal ",
 "pid":0,
 "id":4
 }
]

The following code


 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();
 // Convert the array to Object In the form of, key Is in the array id
 for (int i = 0; i < arr.size(); i++) {
  JSONObject json = (JSONObject) arr.get(i);
  hash.put(json.getString(id), json);
 }
 // Traverse the result set 
 for (int j = 0; j < arr.size(); j++) {
  // A single record 
  JSONObject aVal = (JSONObject) arr.get(j);
  // in hash Remove the key For a single record pid The value of the 
  String pidStr = "";
  Object pidObj = aVal.get(pid);
  if (aVal.get(pid) != null) {
  pidStr = aVal.get(pid).toString();
  }
  JSONObject hashVP = (JSONObject) hash.get(pidStr);
  // If recorded pid If it exists, it has a parent node and adds it to the collection of child nodes 
  if (hashVP != null) {
  // Check if there is child attribute 
  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"," Gansu province ");
 data.add(map);
 Map<String,Object> map2 = new HashMap<>();
 map2.put("id",2);
 map2.put("pid",1);
 map2.put("name"," tianshui ");
 data.add(map2);
 Map<String,Object> map3 = new HashMap<>();
 map3.put("id",3);
 map3.put("pid",2);
 map3.put("name"," The qin state ");
 data.add(map3);
 Map<String,Object> map4 = new HashMap<>();
 map4.put("id",4);
 map4.put("pid",0);
 map4.put("name"," The Beijing municipal ");
 data.add(map4);
 Map<String,Object> map5 = new HashMap<>();
 map5.put("id",5);
 map5.put("pid",4);
 map5.put("name"," Changping district ");
 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));
}

Related articles: