树形结构转换为json类型的字符串



package com.pkpm.zjk.util;
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.pkpm.zjk.entity.Qhdm;
 
public class  TreeToJson{
 
    public  String  ToJson(List<Qhdm> list){
       List dataList = new ArrayList<Qhdm>();
       for(int i=0 ;i<list.size();i++){
         HashMap dataRecord = new HashMap();
         dataRecord.put("id", list.get(i).getQhdm());
            dataRecord.put("text", list.get(i).getQhmc());
            dataRecord.put("parentId",list.get(i).getSjdm());
            dataList.add(dataRecord) ;
       }
       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.toString());           
         // root.sortChildren();
          // 输出有序的树形菜单的JSON字符串
          System.out.println(root.toString());
       return root.toString() ;
      }
   }
 
 
/**
* 节点类
*/
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 + "'";*/
          // + "id : '" + id + "'"
                 + "name : '" + text + "'";
        
        if (children != null && children.getSize() != 0) {
   
            result += ", cityList: " + 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();) {
         String parentId =((Node) it.next()).parentId  ;
         System.out.println(parentId) ;
         System.out.println(parentId.substring(parentId.length()-3)) ;
         if(parentId.substring(parentId.length()-4).equals("0000")){
          result += ((Node) it.next()).toString();
                result += ",";
         }
         else{
            System.out.println("进入else") ;
            result += ((Node) it.next()).toString1();
               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));
    }  
}
 


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值