Java递归算法构造JSON树形结构

1.前言

最近项目中有一个需求,数据库中的菜单表是一个常见的id-pid结构,需要把它构建成树形的JSON格式发送给第三方,写出来之后感觉也是很简单的,核心代码只有5行,重要的是思路要明确,这里把源码分享给大家。

工程里面使用了json-lib.jar这个包,作用是将List序列化成JSON

2.前言

package com.agileai.esb.smc.domain;

 

import java.util.*;

import net.sf.json.JSONArray;

public class TreeBuilder {

        

         List nodes = new ArrayList();

 

         public TreeBuilder(List nodes) {

                   super();

                   this.nodes= nodes;

         }

        

         /**

          * 构建JSON树形结构

          * @return

          */

         public String buildJSONTree() {

                   List nodeTree = buildTree();

                   JSONArray jsonArray = JSONArray.fromObject(nodeTree);

                   return jsonArray.toString();

         }

        

         /**

          * 构建树形结构

          * @return

          */

         public List buildTree() {

                   ListtreeNodes = new ArrayList();

                   ListrootNodes = getRootNodes();

                   for (Node rootNode : rootNodes) {

                            buildChildNodes(rootNode);

                            treeNodes.add(rootNode);

                   }

                   return treeNodes;

         }

        

         /**

          * 递归子节点

          * @param node

          */

         public void buildChildNodes(Node node) {

       List children = getChildNodes(node); 

       if (!children.isEmpty()) {

            for(Node child : children) {

                     buildChildNodes(child);

            } 

            node.setMenus(children);

       }

         }

 

         /**

          * 获取父节点下所有的子节点

          * @param nodes

          * @param pnode

          * @return

          */

         public List getChildNodes(Node pnode) {

                   ListchildNodes = new ArrayList();

                   for (Node n : nodes){

                            if (pnode.getId().equals(n.getPid())) {

                                     childNodes.add(n);

                            }

                   }

                   return childNodes;

         }

        

         /**

          * 判断是否为根节点

          * @param nodes

          * @param inNode

          * @return

          */

         public boolean rootNode(Node node) {

                   boolean isRootNode = true;

                   for (Node n : nodes){

                            if (node.getPid().equals(n.getId())) {

                                     isRootNode= false;

                                     break;

                            }

                   }

                   return isRootNode;

         }

        

         /**

          * 获取集合中所有的根节点

          * @param nodes

          * @return

          */

         public List getRootNodes() {

                   ListrootNodes = new ArrayList();

                   for (Node n : nodes){

                            if (rootNode(n)) {

                                     rootNodes.add(n);

                            }

                   }

                   return rootNodes;

         }

        

         public static class Node {

                  

                   private String id;

                   private String pid;

                   private String text;

                   private String url;

                   private List menus;

                  

                   public Node() {}

 

                   public Node(String id, String pid, String text, String url) {

                            super();

                            this.id =id;

                            this.pid =pid;

                            this.text =text;

                            this.url =url;

                   }

                  

                   public String getId() {

                            return id;

                   }

                   public void setId(String id) {

                            this.id =id;

                   }

                   public String getPid() {

                            return pid;

                   }

                   public void setPid(String pid) {

                            this.pid =pid;

                   }

                   public String getText() {

                            return text;

                   }

                   public void setText(String text) {

                            this.text =text;

                   }

                   public String getUrl() {

                            return url;

                   }

                   public void setUrl(String url) {

                            this.url =url;

                   }

                   public List getMenus() {

                            return menus;

                   }

                   public void setMenus(List menus) {

                            this.menus= menus;

                   }

         }

        

         public static void main(String[] args) {

                  

                   Listnodes = new ArrayList();

                   Node p1 = new Node("01", "","01", "");

                   Node p6 = new Node("02", "","02", "");

                   Node p7 = new Node("0201", "02","0201", "");

                   Node p2 = new Node("0101", "01","0101", "");

                   Node p3 = new Node("0102", "01","0102", "");

                   Node p4 = new Node("010101", "0101","010101", "");

                   Node p5 = new Node("010102", "0101","010102", "");

                   Node p8 = new Node("03", "","03", "");

                   nodes.add(p1);

                   nodes.add(p2);

                   nodes.add(p3);

                   nodes.add(p4);

                   nodes.add(p5);

                   nodes.add(p6);

                   nodes.add(p7);
                   
                   nodes.add(p8);
                  

                   TreeBuilder treeBuilder = new TreeBuilder(nodes);

                   System.out.println(treeBuilder.buildJSONTree());

         }

}


你可能感兴趣的:(java)