现在的位置: 首页 > 综合 > 正文

利用多叉树将数据库中的层次数据转换成树形结构的JSON字符串

2013年02月07日 ⁄ 综合 ⁄ 共 7959字 ⁄ 字号 评论关闭

[代码] [Java]代码

001 package test;
002  
003 import java.util.ArrayList;
004 import java.util.Comparator;
005 import java.util.HashMap;
006 import java.util.Iterator;
007 import java.util.List;
008 import java.util.Map;
009 import java.util.Set;
010 import java.util.Collections;
011  
012 /**
013  * 多叉树类
014 */
015 public class
MultipleTree {
016     public
static void
main(String[] args) {
017         // 读取层次数据结果集列表
018         List dataList = VirtualDataGenerator.getVirtualResult();       
019          
020         // 节点列表(散列表,用于临时存储节点对象)
021         HashMap nodeList =
new HashMap();
022         // 根节点
023         Node root =
null;
024         // 根据结果集构造节点列表(存入散列表)
025         for
(Iterator it = dataList.iterator(); it.hasNext();) {
026             Map dataRecord = (Map) it.next();
027             Node node =
new Node();
028             node.id = (String) dataRecord.get("id");
029             node.text = (String) dataRecord.get("text");
030             node.parentId = (String) dataRecord.get("parentId");
031             nodeList.put(node.id, node);
032         }
033         // 构造无序的多叉树
034         Set entrySet = nodeList.entrySet();
035         for
(Iterator it = entrySet.iterator(); it.hasNext();) {
036             Node node = (Node) ((Map.Entry) it.next()).getValue();
037             if
(node.parentId == null
|| node.parentId.equals(
"")) {
038                 root = node;
039             }
else {
040                 ((Node) nodeList.get(node.parentId)).addChild(node);
041             }
042         }
043         // 输出无序的树形菜单的JSON字符串
044         System.out.println(root.toString());           
045         // 对多叉树进行横向排序
046         root.sortChildren();
047         // 输出有序的树形菜单的JSON字符串
048         System.out.println(root.toString());   
049          
050         // 程序输出结果如下(无序的树形菜单)(格式化后的结果):  
051         //      {
052         //          id : '100000',
053         //          text : '廊坊银行总行',
054         //          children : [
055         //                  {
056         //                  id : '110000',
057         //                  text : '廊坊分行',
058         //                  children : [
059         //                          {
060         //                          id : '113000',
061         //                          text : '廊坊银行开发区支行',
062         //                          leaf : true
063         //                          },
064         //                          {
065         //                          id : '111000',
066         //                          text : '廊坊银行金光道支行',
067         //                          leaf : true
068         //                          },
069         //                          {
070         //                          id : '112000',
071         //                          text : '廊坊银行解放道支行',
072         //                          children : [
073         //                                  {
074         //                                  id : '112200',
075         //                                  text : '廊坊银行三大街支行',
076         //                                  leaf : true
077         //                                  },
078         //                                  {
079         //                                  id : '112100',
080         //                                  text : '廊坊银行广阳道支行',
081         //                                  leaf : true
082         //                                  }
083         //                          ]
084         //                          }
085         //                  ]
086         //                  }
087         //          ]
088         //      }
089  
090         // 程序输出结果如下(有序的树形菜单)(格式化后的结果):
091         //      {
092         //          id : '100000',
093         //          text : '廊坊银行总行',
094         //          children : [
095         //                  {
096         //                  id : '110000',
097         //                  text : '廊坊分行',
098         //                  children : [
099         //                          {
100         //                          id : '111000',
101         //                          text : '廊坊银行金光道支行',
102         //                          leaf : true
103         //                          },
104         //                          {
105         //                          id : '112000',
106         //                          text : '廊坊银行解放道支行',
107         //                          children : [
108         //                                  {
109         //                                  id : '112100',
110         //                                  text : '廊坊银行广阳道支行',
111         //                                  leaf : true
112         //                                  },
113         //                                  {
114         //                                  id : '112200',
115         //                                  text : '廊坊银行三大街支行',
116         //                                  leaf : true
117         //                                  }
118         //                          ]
119         //                          },
120         //                          {
121         //                          id : '113000',
122         //                          text : '廊坊银行开发区支行',
123         //                          leaf : true
124         //                          }
125         //                  ]
126         //                  }
127         //          ]
128         //      }      
129          
130     }
131              
132 }
133  
134  
135 /**
136 * 节点类
137 */
138 class Node {
139     /**
140      * 节点编号
141      */
142     public
String id;
143     /**
144      * 节点内容
145      */
146     public
String text;
147     /**
148      * 父节点编号
149      */
150     public
String parentId;
151     /**
152      * 孩子节点列表
153      */
154     private
Children children = new
Children();
155      
156     // 先序遍历,拼接JSON字符串
157     public
String toString() {     
158         String result =
"{"
159             +
"id : '" + id + "'"
160             +
", text : '" + text +
"'";
161          
162         if
(children != null
&& children.getSize() !=
0) {
163             result +=
", children : " + children.toString();
164         }
else {
165             result +=
", leaf : true";
166         }
167                  
168         return
result + "}";
169     }
170      
171     // 兄弟节点横向排序
172     public
void sortChildren() {
173         if
(children != null
&& children.getSize() !=
0) {
174             children.sortChildren();
175         }
176     }
177      
178     // 添加孩子节点
179     public
void addChild(Node node) {
180         this.children.addChild(node);
181     }
182 }
183  
184 /**
185 * 孩子列表类
186 */
187 class Children {
188     private
List list = new
ArrayList();
189      
190     public
int getSize() {
191         return
list.size();
192     }
193      
194     public
void addChild(Node node) {
195         list.add(node);
196     }
197      
198     // 拼接孩子节点的JSON字符串
199     public
String toString() {
200         String result =
"[";       
201         for
(Iterator it = list.iterator(); it.hasNext();) {
202             result += ((Node) it.next()).toString();
203             result +=
",";
204         }
205         result = result.substring(0, result.length() -
1);
206         result +=
"]";
207         return
result;
208     }
209      
210     // 孩子节点排序
211     public
void sortChildren() {
212         // 对本层节点进行排序
213         // 可根据不同的排序属性,传入不同的比较器,这里传入ID比较器
214         Collections.sort(list,
new NodeIDComparator());
215         // 对每个节点的下一层节点进行排序
216         for
(Iterator it = list.iterator(); it.hasNext();) {
217             ((Node) it.next()).sortChildren();
218         }
219     }
220 }
221  
222 /**
223  * 节点比较器
224  */
225 class NodeIDComparator
implements Comparator {
226     // 按照节点编号比较
227     public
int compare(Object o1, Object o2) {
228         int
j1 = Integer.parseInt(((Node)o1).id);
229         int
j2 = Integer.parseInt(((Node)o2).id);
230         return
(j1 < j2 ? -1
: (j1 == j2 ?
0 : 1));
231     }  
232 }
233  
234 /**
235  * 构造虚拟的层次数据
236  */
237 class VirtualDataGenerator {
238     // 构造无序的结果集列表,实际应用中,该数据应该从数据库中查询获得;
239     public
static List getVirtualResult() {            
240         List dataList =
new ArrayList();
241          
242         HashMap dataRecord1 =
new HashMap();
243         dataRecord1.put("id",
"112000");
244         dataRecord1.put("text",
"廊坊银行解放道支行");
245         dataRecord1.put("parentId",
"110000");
246          
247         HashMap dataRecord2 =
new HashMap();
248         dataRecord2.put("id",
"112200");
249         dataRecord2.put("text",
"廊坊银行三大街支行");
250         dataRecord2.put("parentId",
"112000");
251          
252         HashMap dataRecord3 =
new HashMap();
253         dataRecord3.put("id",
"112100");
254         dataRecord3.put("text",
"廊坊银行广阳道支行");
255         dataRecord3.put("parentId"

抱歉!评论已关闭.