1.前言

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

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

2.源码

package

 

import

import

 

public class

        

nodes = newArrayList<TreeBuilder.Node>();

 

public

super();

this.nodes= nodes;

         }

        

/**

          * 构建JSON树形结构

          * @return

          */

public

                   List<Node>nodeTree = buildTree();

fromObject(nodeTree);

return

         }

        

/**

          * 构建树形结构

          * @return

          */

public

newArrayList<Node>();

                   List<Node>rootNodes = getRootNodes();

for

                            buildChildNodes(rootNode);

                            treeNodes.add(rootNode);

                   }

return

         }

        

/**

          * 递归子节点

          * @param node

          */

public voidbuildChildNodes(Node node) {

       List<Node> children = getChildNodes(node); 

if

for(Node child : children) {

                     buildChildNodes(child);

            } 

            node.setMenus(children);

       }

         }

 

/**

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

          * @param nodes

          * @param pnode

          * @return

          */

public

newArrayList<Node>();

for (Node n : nodes){

if

                                     childNodes.add(n);

                            }

                   }

return

         }

        

/**

          * 判断是否为根节点

          * @param nodes

          * @param inNode

          * @return

          */

public booleanrootNode(Node node) {

boolean isRootNode = true;

for (Node n : nodes){

if

false;

break;

                            }

                   }

return

         }

        

/**

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

          * @param nodes

          * @return

          */

public

newArrayList<Node>();

for (Node n : nodes){

if

                                     rootNodes.add(n);

                            }

                   }

return

         }

        

public staticclass

                  

private String id;

private String pid;

private String text;

private String url;

private List<Node> menus;

                  

public

 

public

super();

this.id

this.pid

this.text

this.url

                   }

                  

public

return id;

                   }

public voidsetId(String id) {

this.id

                   }

public

return pid;

                   }

public voidsetPid(String pid) {

this.pid

                   }

public

return text;

                   }

public voidsetText(String text) {

this.text

                   }

public

return url;

                   }

public voidsetUrl(String url) {

this.url

                   }

public

return menus;

                   }

public voidsetMenus(List<Node> menus) {

this.menus= menus;

                   }

         }

        

public staticvoid

                  

new

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

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

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

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

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

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

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

                  

                   nodes.add(p1);

                   nodes.add(p2);

                   nodes.add(p3);

                   nodes.add(p4);

                   nodes.add(p5);

                   nodes.add(p6);

                   nodes.add(p7);

                  

new

out.println(treeBuilder.buildJSONTree());

         }

}

Java递归算法构造JSON树形结构文档 下载