例子1:
```java
package monitoring.warning.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.hutool.json.JSONUtil;
import monitoring.warning.dto.Test.Dept;
public class DeptTest2Controller {
private static List<Dept> deptList;
static {
Dept dept1 = new Dept(1, 0, "中国");
Dept dept2 = new Dept(2, 1, "北京");
Dept dept3 = new Dept(3, 1, "上海");
Dept dept4 = new Dept(4, 1, "广州");
Dept dept5 = new Dept(5, 1, "重庆");
Dept dept6 = new Dept(6, 5, "九龙坡区");
Dept dept7 = new Dept(7, 6, "一个街道");
deptList = new ArrayList<Dept>();
deptList.add(dept1);
deptList.add(dept2);
deptList.add(dept3);
deptList.add(dept4);
deptList.add(dept5);
deptList.add(dept6);
deptList.add(dept7);
}
private static List<Dept> buildTree(List<Dept> deptList, int pid) {
List<Dept> treeList = new ArrayList<Dept>();
for (Dept dept : deptList) {
if (dept.getParentId() == pid) {
dept.setChild(buildTree(deptList, dept.getId()));
treeList.add(dept);
}
}
return treeList;
}
public static void main(String[] args) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("data", buildTree(deptList, 1));
System.out.println(JSONUtil.toJsonStr(map));
}
}
例子2:
```java
package monitoring.warning.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections4.map.HashedMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class DeptTestController {
public static void main(String[] args) {
Map<String, Node> nodes = new HashedMap();
//模拟数据库存储树结构
nodes.put("1", new Node("1", "root", 1, "0"));
nodes.put("2", new Node("2", "张三", 1, "1"));
nodes.put("3", new Node("3", "李四", 1, "1"));
nodes.put("4", new Node("4", "王五", 1, "1"));
nodes.put("5", new Node("5", "刘六", 1, "1"));
System.out.println("result:" + JSON.toJSONString(getNodeJson("0", nodes)));
}
/**
* 递归处理 数据库树结构数据->树形json
* @param nodeId
* @param nodes
* @return
*/
public static JSONArray getNodeJson(String nodeId, Map<String, Node> nodes) {
//当前层级当前node对象
Node cur = nodes.get(nodeId);
//当前层级当前点下的所有子节点
List<Node> childList = getChildNodes(nodeId, nodes);
JSONArray childTree = new JSONArray();
for (Node node : childList) {
JSONObject o = new JSONObject();
o.put("name", node.getName());
o.put("type", node.getType());
o.put("children", node.getId());
JSONArray childs = getNodeJson(node.getId(), nodes); //递归调用该方法
if (!childs.isEmpty()) {
o.put("children", childs);
}
childTree.fluentAdd(o);
}
return childTree;
}
/**
* 获取当前节点的所有子节点
* @param nodeId
* @param nodes
* @return
*/
public static List<Node> getChildNodes(String nodeId, Map<String, Node> nodes) {
List<Node> list = new ArrayList<>();
for (String key : nodes.keySet()) {
if (nodes.get(key).getParentId().equals(nodeId)) {
list.add(nodes.get(key));
}
}
return list;
}
}
class Node {
public String id;
public String name;
public Integer type;
public String parentId;
public Node(String id, String name, Integer type, String parentId) {
this.id = id;
this.name = name;
this.type = type;
this.parentId = parentId;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
}