bootstrap-treeview 后台拼装JSON

<span style="font-family: Arial, Helvetica, sans-serif;">/**</span>
	 * 初始化数据
	 * @param specTableList
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List initList(List  specTableList){
		List dataList = new ArrayList();  
		HashMap dataRecord = new HashMap();  
		for(int i = 0; i < specTableList.size(); i++) {
			VO VO = (VO)specTableList.get(i);
			dataRecord = new HashMap();  
			dataRecord.put("id",VO.getSpecId());  
			dataRecord.put("text", VO.getSpecName());
			String parentID = VO.getParentId();
			if (StringUtils.isEmpty(parentID)) {
				parentID = "0";
			}
			dataRecord.put("parentId",parentID); 
			dataList.add(dataRecord);
		}
		return dataList;
	}
	
	
	class  Node {
		private String id;
		private String text;
		private String parentId;
		
		/** 
		  * 孩子节点列表 
		  */  
		 private Children children = new Children();  
		   
		 // 先序遍历,拼接JSON字符串  
		 public String toString() {    
		  String result = //"["
		  "{" 
		  + "\"text\":\"" + text + "\","
		  + "\"href\":\"" + id +"\"";  
		  if (children != null && children.getSize() != 0) {
			if (result.contains("nodes")) {
				result += ",";
			}else{
				result += ",\"nodes\":" + children.toString();  
			}
		  }  
		  return result + "}";  
		 }  
		   
		 // 兄弟节点横向排序  
		 public void sortChildren() {  
		  if (children != null && children.getSize() != 0) {  
		   children.sortChildren();  
		  }  
		 }  
		   
		 // 添加孩子节点  
		 public void addChild(Node node) {  
		  this.children.addChild(node);  
		 }  
	}
	
	 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 void sortChildren() {  
		  // 对本层节点进行排序  
		  // 可根据不同的排序属性,传入不同的比较器,这里传入ID比较器  
		  Collections.sort(list, new NodeIDComparator());  
		  // 对每个节点的下一层节点进行排序  
		  for (Iterator it = list.iterator(); it.hasNext();) {  
		   ((Node) it.next()).sortChildren();  
		  }  
		 }  
		   
		/** 
		 * 节点比较器 
		 */  
		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));  
		 }   
		}   
	}
	 
<pre name="code" class="java">public String  init(HttpServletRequest request,HttpServletResponse response) throws Exception{
		List  specTableList = null;
		List list =  cacheMap.get("cacheList");
		if (list!=null && list.size()>0) {
			specTableList = cacheMap.get("cacheList");
		}else{
			specTableList = specTableBo.queryTableList();
			cacheMap.put("cacheList", specTableList);
		}
		List dataList = initList(specTableList);
		// 节点列表(散列表,用于临时存储节点对象)  
		HashMap nodeList = new HashMap();  
		  // 根节点  
		Node root = new  Node();
		root.id = "0";
		root.text= "根节点";
		root.parentId = "";		
		nodeList.put(root.id, root);
		  // 根据结果集构造节点列表(存入散列表)  
		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.toString());     
		return "["+root.toString()+"]";
	}


 



  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值