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<TreeBuilder.Node>nodes = newArrayList<TreeBuilder.Node>();
public TreeBuilder(List<Node> nodes) {
super();
this.nodes= nodes;
}
/**
* 构建JSON树形结构
* @return
*/
public String buildJSONTree() {
List<Node>nodeTree = buildTree();
JSONArrayjsonArray = JSONArray.fromObject(nodeTree);
return jsonArray.toString();
}
/**
* 构建树形结构
* @return
*/
public List<Node> buildTree() {
List<Node>treeNodes = newArrayList<Node>();
List<Node>rootNodes = getRootNodes();
for (Node rootNode : rootNodes) {
buildChildNodes(rootNode);
treeNodes.add(rootNode);
}
return treeNodes;
}
/**
* 递归子节点
* @param node
*/
public voidbuildChildNodes(Node node) {
List<Node> children = getChildNodes(node);
if (!children.isEmpty()) {
for(Node child : children) {
buildChildNodes(child);
}
node.setMenus(children);
}
}
/**
* 获取父节点下所有的子节点
* @param nodes
* @param pnode
* @return
*/
public List<Node> getChildNodes(Nodepnode) {
List<Node>childNodes = newArrayList<Node>();
for (Node n : nodes){
if (pnode.getId().equals(n.getPid())) {
childNodes.add(n);
}
}
return childNodes;
}
/**
* 判断是否为根节点
* @param nodes
* @param inNode
* @return
*/
public booleanrootNode(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<Node> getRootNodes() {
List<Node>rootNodes = newArrayList<Node>();
for (Node n : nodes){
if (rootNode(n)) {
rootNodes.add(n);
}
}
return rootNodes;
}
public staticclass Node {
private String id;
private String pid;
private String text;
private String url;
private List<Node> menus;
public Node() {}
public Node(String id, String pid, Stringtext, String url) {
super();
this.id =id;
this.pid =pid;
this.text =text;
this.url =url;
}
public String getId() {
return id;
}
public voidsetId(String id) {
this.id =id;
}
public String getPid() {
return pid;
}
public voidsetPid(String pid) {
this.pid =pid;
}
public String getText() {
return text;
}
public voidsetText(String text) {
this.text =text;
}
public String getUrl() {
return url;
}
public voidsetUrl(String url) {
this.url =url;
}
public List<Node> getMenus() {
return menus;
}
public voidsetMenus(List<Node> menus) {
this.menus= menus;
}
}
public staticvoid main(String[] args) {
List<Node>nodes = new ArrayList<Node>();
Nodep1 = new Node("01", "","01", "");
Nodep6 = new Node("02", "","02", "");
Nodep7 = new Node("0201", "02","0201", "");
Nodep2 = new Node("0101", "01","0101", "");
Nodep3 = new Node("0102", "01","0102", "");
Nodep4 = new Node("010101", "0101","010101", "");
Nodep5 = 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);
TreeBuildertreeBuilder = new TreeBuilder(nodes);
System.out.println(treeBuilder.buildJSONTree());
}
}
Java递归算法构造JSON树形结构文档 下载