java后端把數(shù)據(jù)轉換為樹,map遞歸生成json樹,返回給前端(后臺轉換)
java 后端,把數(shù)據(jù)轉換為樹,map遞歸生成一顆json樹,返回給前端(后臺轉換)
1.為什么要寫這樣的一篇博客?
2.java 后端代碼
3. 轉化完的數(shù)據(jù)在前端格式為類似于:
1.為什么要寫這樣的一篇博客?
在公司的實習的時候,遇到了一個略坑的東西,就是要醫(yī)院的科室通過其子父id做成一顆項目樹,但是科室的層次有很多,有點甚至到了六層,導致最終選擇了優(yōu)化后的遞歸算法。
如果在三層或者三層以下,可以考慮使用內部類,超過三層的話,最好就使用遞歸了,不過記得必須的優(yōu)化。
2.java 后端代碼
代碼的解釋和理解我卸載代碼里面,返回到前端會自動轉換成Json格式的數(shù)據(jù)。
//第一個參數(shù),需要生成樹的數(shù)組,第二個參數(shù)為樹的根節(jié)點 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)); }; } // 優(yōu)化,減少科室集合的數(shù)量,避免重復查詢,有再優(yōu)化的方法,希望告知。。 json.removeAll(tempJson); for(int i = 0;i < tempJson.size(); i ++) { //對第二層進行遞歸,此處類推 getJsontree(json, tempJson.getJSONObject(i)); } //生成完的樹結構map集合加到根節(jié)點 if(tempJson.size()!=0) job.put("children", tempJson); return job; }
3. 轉化完的數(shù)據(jù)在前端格式為類似于:
[ { text: '節(jié)點1', children: [ { text: '節(jié)點1.1' }, { text: '節(jié)點1.2' }, { text: '節(jié)點1.3', children: [ { text: '節(jié)點1.3.1' }, { text: '節(jié)點1.3.2' } ] }, { text: '節(jié)點1.4' } ] } ]
補充知識:java將list轉為樹形結構的方法
1、通過轉化成json封裝數(shù)據(jù)
[ { "name":"甘肅省", "pid":0, "id":1 }, { "name":"天水市", "pid":1, "id":2 }, { "name":"秦州區(qū)", "pid":2, "id":3 }, { "name":"北京市", "pid":0, "id":4 }, { "name":"昌平區(qū)", "pid":4, "id":5 } ]
現(xiàn)需要是使用java將以上數(shù)據(jù)轉為樹形結構,轉化后下的結構如下
[ { "children":[ { "children":[ { "name":"秦州區(qū)", "pid":2, "id":3 } ], "name":"天水市", "pid":1, "id":2 } ], "name":"甘肅省", "pid":0, "id":1 }, { "children":[ { "name":"昌平區(qū)", "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(); //將數(shù)組轉為Object的形式,key為數(shù)組中的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存在,則說明它有父節(jié)點,將她添加到孩子節(jié)點的集合中 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","秦州區(qū)"); 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","昌平區(qū)"); 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后端把數(shù)據(jù)轉換為樹,map遞歸生成json樹,返回給前端(后臺轉換)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java List接口與Iterator接口及foreach循環(huán)使用解析
這篇文章主要介紹了Java List接口與Iterator接口及foreach循環(huán),主要包括List接口與Iterator接口及foreach循環(huán)具體的使用方法和代碼,需要的朋友可以參考下2022-04-04Java web入門指南之在Idea上創(chuàng)建Java web項目
好多書上的JavaWeb教程都是Eclipse以及MyEclipse,當然這里不論IDE的好壞,下面這篇文章主要給大家介紹了關于Java web入門指南之在Idea上創(chuàng)建Java web項目的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-06-06Java 多線程同步 鎖機制與synchronized深入解析
從尺寸上講,同步代碼塊比同步方法小。你可以把同步代碼塊看成是沒上鎖房間里的一塊用帶鎖的屏風隔開的空間2013-09-09