java 树形结构转换为json字符串

1 篇文章 0 订阅
1 篇文章 0 订阅


package test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class TestTree {
 public  static void  main(String args[]){
    List  dList=new ArrayList()  ;
    HashMap node1 = new HashMap();
    node1.put("NodeId", "1000" );
       node1.put("NodeName","中国" );
       node1.put("ParentId","");
      
       HashMap node2 = new HashMap();
    node2.put("NodeId", "1100" );
       node2.put("NodeName","河南" );
       node2.put("ParentId","1000");
      
       HashMap node21 = new HashMap();
    node21.put("NodeId", "1200" );
       node21.put("NodeName","北京" );
       node21.put("ParentId","1000");
      
       HashMap node211 = new HashMap();
    node211.put("NodeId", "1300" );
       node211.put("NodeName","河北" );
       node211.put("ParentId","1000");
      
       HashMap node3 = new HashMap();
    node3.put("NodeId", "1110" );
       node3.put("NodeName","驻马店" );
       node3.put("ParentId","1100");
      
       HashMap node31 = new HashMap();
    node31.put("NodeId", "1210" );
       node31.put("NodeName","朝阳区" );
       node31.put("ParentId","1200");
      
       HashMap node311 = new HashMap();
    node311.put("NodeId", "1310" );
       node311.put("NodeName","张家口市" );
       node311.put("ParentId","1300");
      
       HashMap node4 = new HashMap();
    node4.put("NodeId", "1111" );
       node4.put("NodeName","正阳" );
       node4.put("ParentId","1110");
      
       HashMap node41 = new HashMap();
    node41.put("NodeId", "1112" );
       node41.put("NodeName","北京***" );
       node41.put("ParentId","1210");
      
       HashMap node411 = new HashMap();
    node411.put("NodeId", "1113" );
       node411.put("NodeName","桥西区" );
       node411.put("ParentId","1310");
      
       dList.add(node1) ;
       dList.add(node2) ;
       dList.add(node3) ;
       dList.add(node4) ;
      
   
       dList.add(node21) ;
       dList.add(node31) ;
       dList.add(node41) ;
      
       dList.add(node211) ;
       dList.add(node311) ;
       dList.add(node411) ;
      
      
       HashMap nodeList = new HashMap();
          Node root = null;
       
          for (int i=0 ;i<dList.size();i++){
              Map dataRecord = (Map) dList.get(i);
              Node node = new Node();
              node.NodeId= (String) dataRecord.get("NodeId");
              node.NodeName = (String) dataRecord.get("NodeName");
              node.ParentId = (String) dataRecord.get("ParentId");
              nodeList.put(node.NodeId, node);
              System.out.print("NodeName:"+node.NodeName+"       ");
              System.out.print("ParentId:"+node.ParentId+"     ");
              System.out.println("NodeId:"+node.NodeId);
             
           }
          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 {
                if(node!=null){
                 nodeList.get(node.ParentId) ;
                   ((Node) nodeList.get(node.ParentId)).addChild(node); 
                   
                }
               
              }
             
          }
 //**************************************************************************************
          System.out.println(root.toString());
          }
}

/*
 *结点数据结构 
 * */
class  Node{
 
 public  String  NodeName   ;
 public  String   NodeId    ;
 public  String   ParentId  ;
 public  Children children =new Children() ;
 public String getNodeName() {
  return NodeName;
 }
 public void addChild(Node node) {
    this.children.addChildren(node);
  }
 public void setNodeName(String nodeName) {
   NodeName = nodeName;
 }
 public String getNodeId() {
  return NodeId;
 }
 public void setNodeId(String nodeId) {
  NodeId = nodeId;
 }
 public String getParentId() {
  return ParentId;
 }
 public void setParentId(String parentId) {
  ParentId = parentId;
 }
 public Children getChildren() {
  return children;
 }
 public void setChildren(Children children) {
  this.children = children;
 }
 public  String toString(){
      String result = "{"
            + "China : '" + NodeName + "'";
           for(int i=0 ;i<children.list.size();i++){
               if (children.list.get(i)!=null) {
          result += ", city: " + children.list.get(i).toString();
          } 
               }
    return result + "}";
          }
}
/*
 * 孩子树形结构
 */
class Children{
 public  List<Node>  list=new ArrayList<Node>()  ;
 public List<Node> getList() {
  return list;
 }
 public void setList(List<Node> list) {
  this.list = list;
 }
 public  void addChildren(Node  node){
  list.add(node) ;
  }
 public String  toString(){
  String result = "{"  ;
        for(int i=0 ;i<list.size();i++){
         result+= "county : '" + list.get(i).NodeName + "' ,  ";
         list.get(i).children.toString() ;
         } 
    return result + "}";
 }
 
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值