java根據資料庫中的資料 的list 產生 tree 型 json

來源:互聯網
上載者:User

標籤:substr   pre   array   obj   jsonview   rtc   null   eid   entryset   

這些代碼 本來 也是 我網上找的但是 很遺憾 ,用資料來源取到的list ,無法正常返回到前台,list 轉json 的時候 就會 出錯 ,我展示 正確的代碼

1.

package com.labci.javamail.test;import java.util.ArrayList;import java.util.Collections;import java.util.Iterator;import java.util.List;/** * 節點類 */  class Node {   /**   * 節點編號   */   public String id;   /**   * 節點內容   */   public String text;   /**   * 父節點編號   */   public String parentId;   /**   * 孩子節點列表   */   private children children = new children();   // 先序遍曆,拼接JSON字串   public String toString() {      String result = "{"     + "id : ‘" + id + "‘"     + ", text : ‘" + text + "‘";    if (children != null && children.getSize() != 0) {     result += ", children : " + children.toString();    } else {     result += ", leaf : true";    }    return result + "}";   }   // 兄弟節點橫向排序   public void sortChildren() {    if (children != null && children.getSize() != 0) {     children.sortChildren();    }   }   // 添加孩子節點   public void addChild(Node node) {    this.children.addChild(node);   }public String getId() {    return id;}public void setId(String id) {    this.id = id;}public String getText() {    return text;}public void setText(String text) {    this.text = text;}public String getParentId() {    return parentId;}public void setParentId(String parentId) {    this.parentId = parentId;}public children getChildren() {    return children;}public void setChildren(children children) {    this.children = children;}  } 

2

package com.labci.javamail.test;import java.util.ArrayList;import java.util.Collections;import java.util.Iterator;import java.util.List;/** * 孩子列表類 */  public class  children {   private List list = new ArrayList();   public int getSize() {    return list.size();   }   public void addChild(Node node) {    list.add(node);   }   // 拼接孩子節點的JSON字串   public String toString() {    String result = "[";      for (Iterator it = list.iterator(); it.hasNext();) {     result += ((Node) it.next()).toString();     result += ",";    }    result = result.substring(0, result.length() - 1);    result += "]";    return result;   }   public List getList() {    return list;}public void setList(List list) {    this.list = list;}// 孩子節點排序   public void sortChildren() {    // 對本層節點進行排序    // 可根據不同的排序屬性,傳入不同的比較子,這裡傳入ID比較子    Collections.sort(list);    // 對每個節點的下一層節點進行排序    for (Iterator it = list.iterator(); it.hasNext();) {     ((Node) it.next()).sortChildren();    }   }  }  

3

package com.labci.javamail.test;import java.util.ArrayList;import java.util.HashMap;import java.util.List;public class VirtualDataGenerator {     // 按照節點編號比較      public static List getVirtualResult() {          List dataList = new ArrayList();          HashMap dataRecord1 = new HashMap();          dataRecord1.put("id", "112000");          dataRecord1.put("text", "廊坊銀行解放道支行");          dataRecord1.put("parentId", "110000");          HashMap dataRecord2 = new HashMap();          dataRecord2.put("id", "112200");          dataRecord2.put("text", "廊坊銀行三大街支行");          dataRecord2.put("parentId", "112000");          HashMap dataRecord3 = new HashMap();          dataRecord3.put("id", "112100");          dataRecord3.put("text", "廊坊銀行廣陽道支行");          dataRecord3.put("parentId", "112000");          HashMap dataRecord4 = new HashMap();          dataRecord4.put("id", "113000");          dataRecord4.put("text", "廊坊銀行開發區支行");          dataRecord4.put("parentId", "110000");          HashMap dataRecord5 = new HashMap();          dataRecord5.put("id", "100000");          dataRecord5.put("text", "廊坊銀行總行");          dataRecord5.put("parentId", "");          HashMap dataRecord6 = new HashMap();          dataRecord6.put("id", "110000");          dataRecord6.put("text", "廊坊分行");          dataRecord6.put("parentId", "100000");          HashMap dataRecord7 = new HashMap();          dataRecord7.put("id", "111000");          dataRecord7.put("text", "廊坊銀行金光道支行");          dataRecord7.put("parentId", "110000");          HashMap dataRecord8 = new HashMap();          dataRecord8.put("id", "111000");          dataRecord8.put("text", "廊坊銀行金光道支行wwwwww");          dataRecord8.put("parentId", "100000");          dataList.add(dataRecord1);          dataList.add(dataRecord2);          dataList.add(dataRecord3);          dataList.add(dataRecord4);          dataList.add(dataRecord5);          dataList.add(dataRecord6);          dataList.add(dataRecord7);          dataList.add(dataRecord8);          System.out.println(dataList);        return dataList;      }  }

4。註:List dataList = VirtualDataGenerator.getVirtualResult(); list 換成自己的 從資料庫中取出的list 就可以了 哈

package com.labci.javamail.test;  import java.util.ArrayList;  import java.util.Comparator;  import java.util.HashMap;  import java.util.Iterator;  import java.util.List;  import java.util.Map;  import java.util.Set;  import java.util.Collections;  import com.labci.javamail.test.VirtualDataGenerator;/**  * 多叉樹類  */  public class MultipleTree {      public static void main(String[] args) {          // 讀取層次資料結果集列表           List dataList = VirtualDataGenerator.getVirtualResult();          // 節點列表(散列表,用於臨時儲存節點對象)          HashMap nodeList = new HashMap();          // 根節點          Node root = null;          // 將結果集存入散列表(後面將藉助散列表構造多叉樹)          for (Iterator it = dataList.iterator(); it.hasNext();) {              Map dataRecord = (Map) it.next();              Node node = new Node();              node.id = (String) dataRecord.get("id");              node.text = (String) dataRecord.get("text");              node.parentId = (String) dataRecord.get("parentId");              nodeList.put(node.id, node);          }          // 構造無序的多叉樹          Set entrySet = nodeList.entrySet();          for (Iterator it = entrySet.iterator(); it.hasNext();) {              Node node = (Node) ((Map.Entry) it.next()).getValue();              if (node.parentId == null || node.parentId.equals("")) {                  root = node;              } else {                  ((Node) nodeList.get(node.parentId)).addChild(node);              }          }          // 輸出無序的樹形菜單的JSON字串          System.out.println(root);          // 對多叉樹進行橫向排序          root.sortChildren();          // 輸出有序的樹形菜單的JSON字串          System.out.println(root);          // 程式輸出結果如下:          //          // 無序的樹形菜單(格式化後的結果,可使用JSON格式化工具查看,          // 例如 http://jsonviewer.stack.hu/ 線上查看器):            //  {          //   id : ‘100000‘,           //   text : ‘廊坊銀行總行‘,           //   children : [          //     {          //     id : ‘110000‘,           //     text : ‘廊坊分行‘,           //     children : [          //       {          //       id : ‘113000‘,           //       text : ‘廊坊銀行開發區支行‘,           //       leaf : true          //       },          //       {          //       id : ‘111000‘,           //       text : ‘廊坊銀行金光道支行‘,           //       leaf : true          //       },          //       {          //       id : ‘112000‘,           //       text : ‘廊坊銀行解放道支行‘,           //       children : [          //         {          //         id : ‘112200‘,           //         text : ‘廊坊銀行三大街支行‘,           //         leaf : true          //         },          //         {          //         id : ‘112100‘,           //         text : ‘廊坊銀行廣陽道支行‘,           //         leaf : true          //         }          //       ]          //       }          //     ]          //     }          //   ]          //  }          // 有序的樹形菜單(格式化後的結果):          //  {          //   id : ‘100000‘,           //   text : ‘廊坊銀行總行‘,           //   children : [          //     {          //     id : ‘110000‘,           //     text : ‘廊坊分行‘,           //     children : [          //       {          //       id : ‘111000‘,           //       text : ‘廊坊銀行金光道支行‘,           //       leaf : true          //       },          //       {          //       id : ‘112000‘,           //       text : ‘廊坊銀行解放道支行‘,           //       children : [          //         {          //         id : ‘112100‘,           //         text : ‘廊坊銀行廣陽道支行‘,           //         leaf : true          //         },          //         {          //         id : ‘112200‘,           //         text : ‘廊坊銀行三大街支行‘,           //         leaf : true          //         }          //       ]          //       },          //       {          //       id : ‘113000‘,           //       text : ‘廊坊銀行開發區支行‘,           //       leaf : true          //       }          //     ]          //     }          //   ]          //  }        }  }  /**  * 節點比較子  */  class NodeIDComparator implements Comparator {      // 按照節點編號比較      public int compare(Object o1, Object o2) {          int j1 = Integer.parseInt(((Node) o1).id);          int j2 = Integer.parseInt(((Node) o2).id);          return (j1 < j2 ? -1 : (j1 == j2 ? 0 : 1));      }  }  

java根據資料庫中的資料 的list 產生 tree 型 json

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.