之前项目有个需求,前台需要展示一个类似
[{“path”:"/dashboard",“children”:[{“path”:“aa”,“children”:[{“path”:“bb”,“children”:[],“name”:“bb”,“id”:“100101”,“parentid”:“1001”}],“name”:“aa”,“id”:“1001”,“parentid”:“1”}],“name”:“dashboard”,“id”:“1”,“parentid”:""},{“path”:“http://www.anline.cn”,“children”:[],“name”:“安浪创想”,“id”:“2”,“parentid”:""}]这儿样的json树结构,通过查找资料自己写了一个工具类如下:
package com.law.firm.com_law_firm.entity;
import com.sun.deploy.panel.TreeBuilder;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import java.util.ArrayList;
import java.util.List;
/**
-
@program: com_law_firm
-
@description: 组装json树
-
author:
-
@create: 2019-08-23 09-10
/
public class JsonTreeUtils {
/- 获取集合中所有数据
- @param nodes
- @return 所有根节点的集合
*/
public static List<Children_node> getRootNodes(List<Children_node> nodes) {
// List<Children_node> nodes = children_nodeMapper.selectByParenid(“2”);
List<Children_node>rootNodes = new ArrayList<Children_node>();
for (Children_node n : nodes){
//判断是不是根节点
if (rootNode(n,nodes)) {
rootNodes.add(n);
}
}
//所有的根节点的集合
return rootNodes;
}
/**- 判断是否为根节点
- @param nodes
- @param inNode
- @return
/
public static boolean rootNode(Children_node node,List<Children_node> nodes) {
boolean isRootNode = true;
for (Children_node n : nodes){
if (node.getParentid().equals(n.getId())) {
isRootNode= false;
break;
}
}
return isRootNode;
}
/* - 构建树形结构
- @param nodes 所有的节点(集合中的所有数据)
- @return
*/
public static List< Children_node> buildTree(List<Children_node> nodes) {
List<Children_node> treeNodes = new ArrayList<Children_node>();
// 获取所有的根节点的数据
List<Children_node>rootNodes = getRootNodes(nodes);
for (Children_node rootNode : rootNodes) {
buildChildNodes(rootNode,nodes);
treeNodes.add(rootNode);
}
return treeNodes;
}
/**- 构建JSON树形结构
- @param nodes 集合所有的数据
- @return json树结构
/
public static String buildJSONTree(List<Children_node> nodes) {
List<Children_node> nodeTree = buildTree(nodes);
JSONArray jsonArray = JSONArray.fromObject(nodeTree);
return jsonArray.toString();
}
/* - 递归子节点
- @param node 根节点对象
- @param nodes 所有集合的对象
/
public static void buildChildNodes(Children_node node,List<Children_node> nodes) {
// 根据根节点对象获取其所有的子节点对象,参数为根节点对象
List<Children_node> childrens = getChildNodes(node, nodes);
if (!childrens.isEmpty()) {
for(Children_node child : childrens) {
buildChildNodes(child,nodes);
}
node.setChildren(childrens);
}
}
/* - 获取父节点下所有的子节点
- @param nodes 集合中的所有对象
- @param pnode 根节点对象
- @return
*/
public static List<Children_node> getChildNodes(Children_node pnode,List<Children_node> nodes) {
// 所有子节点的集合
List<Children_node>childNodes = new ArrayList<Children_node>();for (Children_node n : nodes){ if (pnode.getId().equals(n.getParentid())) { childNodes.add(n); } } //当前父节点所有子节点的集合 return childNodes;
}
}
经过测试得到结果如图:
如果工具类做如下修改为:
public static String buildJSONTree(List<Children_node> nodes) {
JSONArray js = JSONArray.fromObject(nodes);
return js.toString();
JSONArray js1 = new JSONArray();
JSONArray js2 = new JSONArray();
List<Children_node> nodeChild = new ArrayList<>();
List<Children_node> nodeTree = buildTree(nodes);
for (Children_node ch:nodeTree ) {
js2= get(ch);
if (ch.getChildren() != null) {
for (Children_node child:ch.getChildren() ) {
JSONObject jchild=new JSONObject();
jchild.put(“id”,child.getId());
jchild.put(“path”,child.getPath());
jchild.put(“name”,child.getName());
jchild.put(“parentid”,child.getParentid());
jchild.put(“children”,child.getChildren());
js2.add(jchild);
}
}
JSONObject j=new JSONObject();
j.put(“id”,ch.getId());
j.put(“path”,ch.getPath());
j.put(“name”,ch.getName());
j.put(“parentid”,ch.getParentid());
j.put(“children”,js2);
js1.add(j);
}
return js1.toString();
}
// 递归排序子节点
public static JSONArray get(Children_node children_node) {
JSONArray js2 = new JSONArray();
if(children_node.getChildren()!=null) {
for (Children_node child:children_node.getChildren() ) {
JSONObject jchild=new JSONObject();
jchild.put(“id”,child.getId());
jchild.put(“path”,child.getPath());
jchild.put(“name”,child.getName());
jchild.put(“parentid”,child.getParentid());
jchild.put(“children”,child.getChildren());
js2.add(jchild);
get(child);
}
}
return js2;
}
如此来得到的数据跟对象的属性顺序也是一一对应的