本文展示了两个实现方法的代码。两个代码的实现方法不同,代码2更为简单。
先看一下最后实现的结果:
最后结果-json
代码1:
实现过程:
1.传入一段json字符串
2.将字符串转换成对象存入节点列表
3.根据节点列表构造无序的多叉树,并将个孩子节点加入对应的父节点中,将无对应父节点的节点加入一级节点列表
4.排序
代码:
package com.example.rabbitmq2; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import java.util.*; public class MultipleTree {
public static void main(String[] args) { /**
* 读取层次数据结果集列表
*/
String strJson ="[\n" +"" +
" {\n" +
" \"id\":50090113,\n" +
" \"parentId\":50000001,\n" +
" \"name\":\"应用技术研发二中心\",\n" +
" \"status\":0, \n" +
" \"fullName\":\"应用技术研发二中心\"\n" +
" },\n" +
" {\n" +
" \"id\":50090193,\n" +
" \"parentId\":50000000,\n" +
" \"name\":\"应用技术研发一中心\",\n" +
" \"status\":0, \n" +
" \"fullName\":\"应用技术研发一中心\"\n" +
" },\n" +
" {\n" +
" \"id\":50090194,\n" +
" \"parentId\":50090193,\n" +
" \"name\":\"前端研发部\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部\"\n" +
" },\n" +
" {\n" +
" \"id\":50005413,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"安全网关产品前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/安全网关产品前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50005415,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"前端研发二组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/前端研发二组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090199,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"云安全前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/云安全前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090200,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"威胁情报前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/威胁情报前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090201,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据与安全运营前端产品组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端产品组\"\n" +
" \n" +
" },\n" +
" {\n" +
" \"id\":50090202,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据与安全运营前端架构组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端架构组\"\n" +
" \n" +
" },\n" +
" {\n" +
" \"id\":50090203,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据与安全运营前端研发二组\",\n" +
" \"status\":1,\n" +
" \n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端研发二组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090204,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据与安全运营前端研发三组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端研发三组\"\n" +
"\n" +
" },\n" +
" {\n" +
" \"id\":50090205,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据与安全运营前端研发四组\",\n" +
" \"status\":1,\n" +
" \n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端研发四组\"\n" +
"\n" +
" },\n" +
" {\n" +
" \"id\":50090206,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据与安全运营前端研发运营商部\",\n" +
" \"status\":1,\n" +
" \n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端研发运营商部\"\n" +
"\n" +
" },\n" +
" {\n" +
" \"id\":50090207,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据与安全运营前端研发政企部\",\n" +
" \"status\":1, \n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端研发政企部\"\n" +
" },\n" +
" {\n" +
" \"id\":50090208,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"态势感知前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/态势感知前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090209,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据应用前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据应用前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090210,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据与威胁分析前端研发一组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据与威胁分析前端研发一组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090211,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据与威胁公司前端研发二组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据与威胁公司前端研发二组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090212,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据与威胁分析前端研发三组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据与威胁分析前端研发三组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090213,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"工业安全前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/工业安全前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090214,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"行为安全前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/行为安全前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090215,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"基础大数据前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/基础大数据前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090216,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"应用产品前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/应用产品前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090217,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"终端安全产品前端研发组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/终端安全产品前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090218,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"安全产品前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/安全产品前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090219,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"数据安全前端研发组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/数据安全前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090220,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"运维前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/运维前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090221,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"华南基地前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/华南基地前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090222,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"补天产品前端研发部\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/补天产品前端研发部\"\n" +
" },\n" +
" {\n" +
" \"id\":50090789,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"前端一部\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端一部\"\n" +
" },\n" +
" {\n" +
" \"id\":50090790,\n" +
" \"parentId\":50090789,\n" +
" \"name\":\"前端一组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端一部/前端一组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090795,\n" +
" \"parentId\":50090789,\n" +
" \"name\":\"前端三组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端一部/前端三组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090797,\n" +
" \"parentId\":50090789,\n" +
" \"name\":\"珠海组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端一部/珠海组\"\n" +
" },\n" +
" {\n" +
" \"id\":50091815,\n" +
" \"parentId\":50090789,\n" +
" \"name\":\"西安组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端一部/西安组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090793,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"前端二部\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端二部\"\n" +
" },\n" +
" {\n" +
" \"id\":50090791,\n" +
" \"parentId\":50090793,\n" +
" \"name\":\"前端四组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端二部/前端四组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090794,\n" +
" \"parentId\":50090793,\n" +
" \"name\":\"前端二组\",\n" +
" \"status\":0,\n" +
"\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端二部/前端二组\"\n" +
" },\n" +
" {\n" +
" \"id\":50091816,\n" +
" \"parentId\":50090793,\n" +
" \"name\":\"上海组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端二部/上海组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090798,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"共性技术组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/共性技术组\"\n" +
" },\n" +
" {\n" +
" \"id\":50091814,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"前端三部\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端三部\"\n" +
" },\n" +
" {\n" +
" \"id\":50090792,\n" +
" \"parentId\":50091814,\n" +
" \"name\":\"前端六组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端三部/前端六组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090796,\n" +
" \"parentId\":50091814,\n" +
" \"name\":\"前端七组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端三部/前端七组\"\n" +
" },\n" +
" {\n" +
" \"id\":50091817,\n" +
" \"parentId\":50091814,\n" +
" \"name\":\"前端八组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端三部/前端八组\"\n" +
" },\n" +
" {\n" +
" \"id\":50091818,\n" +
" \"parentId\":50091814,\n" +
" \"name\":\"前端九组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端三部/前端九组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090195,\n" +
" \"parentId\":50090193,\n" +
" \"name\":\"研发一部\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/研发一部\"\n" +
" },\n" +
" {\n" +
" \"id\":50090620,\n" +
" \"parentId\":50090195,\n" +
" \"name\":\"策略管理组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/研发一部/策略管理组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090621,\n" +
" \"parentId\":50090195,\n" +
" \"name\":\"集中管理组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/研发一部/集中管理组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090622,\n" +
" \"parentId\":50090195,\n" +
" \"name\":\"产品规划与设计组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/研发一部/产品规划与设计组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090632,\n" +
" \"parentId\":50090195,\n" +
" \"name\":\"研发管理组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/研发一部/研发管理组\"\n" +
" },\n" +
" {\n" +
" \"id\":50091022,\n" +
" \"parentId\":50090195,\n" +
" \"name\":\"解决方案组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/研发一部/解决方案组\"\n" +
" },\n" +
" {\n" +
" \"id\":50091023,\n" +
" \"parentId\":50090195,\n" +
" \"name\":\"公共服务组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/研发一部/公共服务组\"\n" +
"\n" +
" },\n" +
" {\n" +
" \"id\":50091024,\n" +
" \"parentId\":50090195,\n" +
" \"name\":\"网络拓普组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/研发一部/网络拓普组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090197,\n" +
" \"parentId\":50090193,\n" +
" \"name\":\"研发三部\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/研发三部\"\n" +
" },\n" +
" {\n" +
" \"id\":50090232,\n" +
" \"parentId\":50090197,\n" +
" \"name\":\"工业安全服务端研发组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/研发三部/工业安全服务端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090631,\n" +
" \"parentId\":50090193,\n" +
" \"name\":\"设计与研发支撑部\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/设计与研发支撑部\"\n" +
" },\n" +
" {\n" +
" \"id\":50090633,\n" +
" \"parentId\":50090193,\n" +
" \"name\":\"UED\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/UED\"\n" +
" },\n" +
" {\n" +
" \"id\":50090799,\n" +
" \"parentId\":50090193,\n" +
" \"name\":\"武汉研发部\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/武汉研发部\"\n" +
" },\n" +
" {\n" +
" \"id\":50090800,\n" +
" \"parentId\":50090799,\n" +
" \"name\":\"武汉前端一组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/武汉研发部/武汉前端一组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090801,\n" +
" \"parentId\":50090799,\n" +
" \"name\":\"武汉前端二组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/武汉研发部/武汉前端二组\"\n" +
" }]\n";
//字符串转换JSON数组
JSONArray jsonArray = JSONArray.parseArray(strJson);
//节点列表(散列表,用于临时存储节点对象)
HashMap nodeList = new HashMap();
//最后结果节点列表
HashMap nodeResultList = new HashMap();
//根节点
Node root = new Node();
Node node =new Node();
//设置根节点,id为0
nodeResultList.put(0, node); //根据结果集构造节点列表(存入散列表)
for(int i=0;i<jsonArray.size();i++){
Node node1 = JSONObject.parseObject(jsonArray.get(i).toString(),Node.class);
nodeList.put(node1.id, node1);
} //构造无序的多叉树 //entrySet() 该方法返回值是这个map中各个键值对映射关系的集合
Set entrySet = nodeList.entrySet();
//value=entrySet.iterator().next().getValue() key=entrySet.iterator().next().getKey()
for (Iterator it = entrySet.iterator(); it.hasNext();) {
node = (Node) ((Map.Entry) it.next()).getValue();
int i= 1;
//如果parentId有对应的id,则作为孩子节点加入对应的父节点中
for (Iterator it2 = entrySet.iterator(); it2.hasNext();) {
Node node2 = (Node) ((Map.Entry) it2.next()).getValue();
if (node.parentId == node2.id) {
((Node) nodeList.get(node.parentId)).addChild(node);
System.out.println(node.parentId);
i++;
break;
}
}
//若parentId都无对应的id,则为一级节点,加入根节点中
if(i==1){
((Node) nodeResultList.get(0)).addChild(node);
}
} root = (Node) nodeResultList.get(0);
// 输出无序的树形菜单的JSON字符串
System.out.println(JSON.toJSONString(root));
// 对多叉树进行横向排序
root.sortChildren(); // 输出有序的树形菜单的JSON字符串
//重写的toString方法的结果,(可使最后结果根据自己想要展示的样子展示)
System.out.println("------------------root.toString()"+root.toString());
//未重写toString方法的结果
System.out.println("------------------JSON.toJSONString(root)"+JSON.toJSONString(root)); }
}
package com.example.rabbitmq2; import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.Getter;
import lombok.Setter; import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List; @Data
public class Node {
/**
* 节点编号
*/
public int id;
/**
* 父节点编号
*/
public int parentId;
/**
* 节点名称
*/
public String name;
/**
* 节点状态
*/
public int status;
/**
* 节点全称
*/
public String fullName; /**
* 孩子节点列表
*/
private List childrenList = new ArrayList(); // 先序遍历,拼接JSON字符串
public String toString() {
String result = "{"
+ "\"id\" : \"" + id + "\""
+ ", \"fullName\" : \"" + fullName + "\""
+ ", \"name\" : \"" + name + "\""
+ ", \"status\" : \"" + status + "\""; if (childrenList != null && childrenList.size() != 0) {
result += ", \"children\" : " + childrenList.toString();
}
return result + "}";
} // 兄弟节点横向排序
public void sortChildren() {
if (childrenList != null && childrenList.size() != 0) {
// 对本层节点进行排序
// 可根据不同的排序属性,传入不同的比较器,这里传入ID比较器
Collections.sort(childrenList, new NodeIDComparator());
// 对每个节点的下一层节点进行排序
for (Iterator it = childrenList.iterator(); it.hasNext();) {
((Node) it.next()).sortChildren();
}
}
}
// 添加孩子节点
public void addChild(Node node) {
childrenList.add(node); }
}
package com.example.rabbitmq2; import java.util.Comparator; /**
* 节点比较器
*/
class NodeIDComparator implements Comparator {
// 按照节点编号比较
public int compare(Object o1, Object o2) {
int j1 = ((Node) o1).id;
int j2 = ((Node) o2).id;
return (j1 < j2 ? -1 : (j1 == j2 ? 0 : 1));
}
}
代码2:
实现过程:
1.将传入的json字符串转为List
2.找的所有的一级节点存入结果list中
3.使用递归,为所有的节点设置子节点
代码
package com.example.rabbitmq2; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.junit.platform.commons.util.StringUtils; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class MultipleTree3 {
public static void main(String[] args) {
// 原始的数据
/**
* 读取层次数据结果集列表
*/
String strJson ="[\n" +"" +
" {\n" +
" \"id\":50090113,\n" +
" \"parentId\":50000001,\n" +
" \"name\":\"应用技术研发二中心\",\n" +
" \"status\":0, \n" +
" \"fullName\":\"应用技术研发二中心\"\n" +
" },\n" +
" {\n" +
" \"id\":50090193,\n" +
" \"parentId\":50000000,\n" +
" \"name\":\"应用技术研发一中心\",\n" +
" \"status\":0, \n" +
" \"fullName\":\"应用技术研发一中心\"\n" +
" },\n" +
" {\n" +
" \"id\":50090194,\n" +
" \"parentId\":50090193,\n" +
" \"name\":\"前端研发部\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部\"\n" +
" },\n" +
" {\n" +
" \"id\":50005413,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"安全网关产品前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/安全网关产品前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50005415,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"前端研发二组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/前端研发二组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090199,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"云安全前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/云安全前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090200,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"威胁情报前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/威胁情报前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090201,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据与安全运营前端产品组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端产品组\"\n" +
" \n" +
" },\n" +
" {\n" +
" \"id\":50090202,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据与安全运营前端架构组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端架构组\"\n" +
" \n" +
" },\n" +
" {\n" +
" \"id\":50090203,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据与安全运营前端研发二组\",\n" +
" \"status\":1,\n" +
" \n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端研发二组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090204,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据与安全运营前端研发三组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端研发三组\"\n" +
"\n" +
" },\n" +
" {\n" +
" \"id\":50090205,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据与安全运营前端研发四组\",\n" +
" \"status\":1,\n" +
" \n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端研发四组\"\n" +
"\n" +
" },\n" +
" {\n" +
" \"id\":50090206,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据与安全运营前端研发运营商部\",\n" +
" \"status\":1,\n" +
" \n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端研发运营商部\"\n" +
"\n" +
" },\n" +
" {\n" +
" \"id\":50090207,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据与安全运营前端研发政企部\",\n" +
" \"status\":1, \n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据与安全运营前端研发政企部\"\n" +
" },\n" +
" {\n" +
" \"id\":50090208,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"态势感知前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/态势感知前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090209,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据应用前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据应用前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090210,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据与威胁分析前端研发一组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据与威胁分析前端研发一组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090211,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据与威胁公司前端研发二组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据与威胁公司前端研发二组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090212,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"大数据与威胁分析前端研发三组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/大数据与威胁分析前端研发三组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090213,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"工业安全前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/工业安全前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090214,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"行为安全前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/行为安全前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090215,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"基础大数据前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/基础大数据前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090216,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"应用产品前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/应用产品前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090217,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"终端安全产品前端研发组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/终端安全产品前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090218,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"安全产品前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/安全产品前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090219,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"数据安全前端研发组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/数据安全前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090220,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"运维前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/运维前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090221,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"华南基地前端研发组\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/华南基地前端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090222,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"补天产品前端研发部\",\n" +
" \"status\":1,\n" +
" \"fullName\":\"应用技术研发中心/前端研发部/补天产品前端研发部\"\n" +
" },\n" +
" {\n" +
" \"id\":50090789,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"前端一部\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端一部\"\n" +
" },\n" +
" {\n" +
" \"id\":50090790,\n" +
" \"parentId\":50090789,\n" +
" \"name\":\"前端一组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端一部/前端一组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090795,\n" +
" \"parentId\":50090789,\n" +
" \"name\":\"前端三组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端一部/前端三组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090797,\n" +
" \"parentId\":50090789,\n" +
" \"name\":\"珠海组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端一部/珠海组\"\n" +
" },\n" +
" {\n" +
" \"id\":50091815,\n" +
" \"parentId\":50090789,\n" +
" \"name\":\"西安组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端一部/西安组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090793,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"前端二部\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端二部\"\n" +
" },\n" +
" {\n" +
" \"id\":50090791,\n" +
" \"parentId\":50090793,\n" +
" \"name\":\"前端四组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端二部/前端四组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090794,\n" +
" \"parentId\":50090793,\n" +
" \"name\":\"前端二组\",\n" +
" \"status\":0,\n" +
"\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端二部/前端二组\"\n" +
" },\n" +
" {\n" +
" \"id\":50091816,\n" +
" \"parentId\":50090793,\n" +
" \"name\":\"上海组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端二部/上海组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090798,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"共性技术组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/共性技术组\"\n" +
" },\n" +
" {\n" +
" \"id\":50091814,\n" +
" \"parentId\":50090194,\n" +
" \"name\":\"前端三部\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端三部\"\n" +
" },\n" +
" {\n" +
" \"id\":50090792,\n" +
" \"parentId\":50091814,\n" +
" \"name\":\"前端六组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端三部/前端六组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090796,\n" +
" \"parentId\":50091814,\n" +
" \"name\":\"前端七组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端三部/前端七组\"\n" +
" },\n" +
" {\n" +
" \"id\":50091817,\n" +
" \"parentId\":50091814,\n" +
" \"name\":\"前端八组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端三部/前端八组\"\n" +
" },\n" +
" {\n" +
" \"id\":50091818,\n" +
" \"parentId\":50091814,\n" +
" \"name\":\"前端九组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/前端研发部/前端三部/前端九组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090195,\n" +
" \"parentId\":50090193,\n" +
" \"name\":\"研发一部\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/研发一部\"\n" +
" },\n" +
" {\n" +
" \"id\":50090620,\n" +
" \"parentId\":50090195,\n" +
" \"name\":\"策略管理组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/研发一部/策略管理组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090621,\n" +
" \"parentId\":50090195,\n" +
" \"name\":\"集中管理组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/研发一部/集中管理组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090622,\n" +
" \"parentId\":50090195,\n" +
" \"name\":\"产品规划与设计组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/研发一部/产品规划与设计组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090632,\n" +
" \"parentId\":50090195,\n" +
" \"name\":\"研发管理组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/研发一部/研发管理组\"\n" +
" },\n" +
" {\n" +
" \"id\":50091022,\n" +
" \"parentId\":50090195,\n" +
" \"name\":\"解决方案组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/研发一部/解决方案组\"\n" +
" },\n" +
" {\n" +
" \"id\":50091023,\n" +
" \"parentId\":50090195,\n" +
" \"name\":\"公共服务组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/研发一部/公共服务组\"\n" +
"\n" +
" },\n" +
" {\n" +
" \"id\":50091024,\n" +
" \"parentId\":50090195,\n" +
" \"name\":\"网络拓普组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/研发一部/网络拓普组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090197,\n" +
" \"parentId\":50090193,\n" +
" \"name\":\"研发三部\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/研发三部\"\n" +
" },\n" +
" {\n" +
" \"id\":50090232,\n" +
" \"parentId\":50090197,\n" +
" \"name\":\"工业安全服务端研发组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/研发三部/工业安全服务端研发组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090631,\n" +
" \"parentId\":50090193,\n" +
" \"name\":\"设计与研发支撑部\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/设计与研发支撑部\"\n" +
" },\n" +
" {\n" +
" \"id\":50090633,\n" +
" \"parentId\":50090193,\n" +
" \"name\":\"UED\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/UED\"\n" +
" },\n" +
" {\n" +
" \"id\":50090799,\n" +
" \"parentId\":50090193,\n" +
" \"name\":\"武汉研发部\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/武汉研发部\"\n" +
" },\n" +
" {\n" +
" \"id\":50090800,\n" +
" \"parentId\":50090799,\n" +
" \"name\":\"武汉前端一组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/武汉研发部/武汉前端一组\"\n" +
" },\n" +
" {\n" +
" \"id\":50090801,\n" +
" \"parentId\":50090799,\n" +
" \"name\":\"武汉前端二组\",\n" +
" \"status\":0,\n" +
" \"fullName\":\"应用技术研发一中心/武汉研发部/武汉前端二组\"\n" +
" }]\n";
//字符串转换为List
JSONArray jsonArray = JSONArray.parseArray(strJson);
List<Node> rootNode = (List<Node>)JSONArray.parseArray(strJson,Node.class);
// 查看结果
// for (Node node : rootNode) {
// System.out.println(node);
// }
// 最后的结果
List<Node> nodeList = new ArrayList<>();
// 先找到所有的一级节点
for (int i = 0; i < rootNode.size(); i++) {
int index = 1;
for (int j = 0; j < rootNode.size();j++) {
if(rootNode.get(i).getParentId()==rootNode.get(j).getId()){
index++;
}
}
//如果该ParentId无相等的id,则index仍然为1。将一级节点加入最后结果List中
if(index==1){
nodeList.add(rootNode.get(i));
}
}
// 为一级节点设置子节点,getChild是递归调用的
for (Node node : nodeList) {
node.setChildrenList(getChild(node.getId(), rootNode));
}
Map<String,Object> jsonMap = new HashMap<>();
jsonMap.put("node", nodeList);
System.out.println(JSONObject.toJSONString(jsonMap));
} private static List getChild(int id, List<Node> rootNode) {
// 子节点
List<Node> childList = new ArrayList<>();
for (Node node : rootNode) {
// 遍历所有节点,如果该节点的父类id等于参数id,则作为孩子节点加入该节点中
if (node.getParentId()==id) {
childList.add(node);
} }
// 把孩子节点的孩子节点循环一遍
for (Node node : childList) {
// 递归
node.setChildrenList(getChild(node.getId(), rootNode));
} // 递归退出条件
if (childList.size() == 0) {
return null;
}
return childList;
} }
package com.example.rabbitmq2; import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.Getter;
import lombok.Setter; import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List; @Data
public class Node {
/**
* 节点编号
*/
public int id;
/**
* 父节点编号
*/
public int parentId;
/**
* 节点名称
*/
public String name;
/**
* 节点状态
*/
public int status;
/**
* 节点全称
*/
public String fullName; /**
* 孩子节点列表
*/
private List childrenList = new ArrayList();
}
相关的博客: