1,TreeNode对象
package com.ruoyi.system.utils.Vo;
import java.util.ArrayList;
import java.util.List;
public class TreeNode {
private Integer id;
private String name;
private Integer parentId;
private List childrenList = new ArrayList();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id=id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId=parentId;
}
public List getChildrenList() {
return childrenList;
}
public void setChildrenList(List childrenList) {
this.childrenList=childrenList;
}
}
2,TreeUtil
package com.ruoyi.system.utils;
import com.ruoyi.system.utils.Vo.TreeNode;
import java.util.ArrayList;
import java.util.List;
public class TreeUtil {
private List<TreeNode> treeNodeList =new ArrayList<>();
public TreeUtil(List<TreeNode> list){
treeNodeList = list;
}
public TreeNode getNodeById(int nodeId){
TreeNode treeNode = new TreeNode();
for (TreeNode item : treeNodeList) {
if (item.getId() == nodeId) {
treeNode = item;
break;
}
}
return treeNode;
}
public List<TreeNode> getChildrenNodeById(int nodeId){
List<TreeNode> childrenTreeNode = new ArrayList<TreeNode>();
for (TreeNode item : treeNodeList) {
if(item.getParentId() == nodeId){
childrenTreeNode.add(item);
}
}
return childrenTreeNode;
}
public TreeNode generateTreeNode(int rootId){
TreeNode root = this.getNodeById(rootId);
List<TreeNode> childrenTreeNode = this.getChildrenNodeById(rootId);
for (TreeNode item : childrenTreeNode) {
TreeNode node = this.generateTreeNode(item.getId());
root.getChildrenList().add(node);
}
return root;
}
public static void main(String[] args) {
List<TreeNode> nodeList = new ArrayList<>();
TreeNode treeNode = new TreeNode();
treeNode.setId(1);
treeNode.setName("中国");
treeNode.setParentId(0);
nodeList.add(treeNode);
treeNode = new TreeNode();
treeNode.setId(2);
treeNode.setName("北京市");
treeNode.setParentId(1);
nodeList.add(treeNode);
treeNode = new TreeNode();
treeNode.setId(3);
treeNode.setName("浙江省");
treeNode.setParentId(1);
nodeList.add(treeNode);
treeNode = new TreeNode();
treeNode.setId(4);
treeNode.setName("杭州市");
treeNode.setParentId(3);
nodeList.add(treeNode);
treeNode = new TreeNode();
treeNode.setId(5);
treeNode.setName("西湖区");
treeNode.setParentId(4);
nodeList.add(treeNode);
treeNode = new TreeNode();
treeNode.setId(6);
treeNode.setName("滨江区");
treeNode.setParentId(4);
nodeList.add(treeNode);
treeNode = new TreeNode();
treeNode.setId(7);
treeNode.setName("金华市");
treeNode.setParentId(3);
nodeList.add(treeNode);
TreeUtil treeUtil = new TreeUtil(nodeList);
treeNode = treeUtil.generateTreeNode(1);
System.out.println("树状结构Json格式数据:" + JsonUtils.objectToJson(treeNode, 1));
}
}
3,JsonUtil
package com.ruoyi.system.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
public class JsonUtils {
private static final ObjectMapper MAPPER = new ObjectMapper();
public static <T> String objectToJson(T obj, int flag) {
if (obj == null) {
return null;
}
try {
String string;
if (flag == 1) {
string = obj instanceof String ? (String) obj : MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
} else {
string = obj instanceof String ? (String) obj : MAPPER.writeValueAsString(obj);
}
return string;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
try {
T t = MAPPER.readValue(jsonData, beanType);
return t;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static <T>List<T> jsonToList(String jsonData, Class<T> beanType) {
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
try {
List<T> list = MAPPER.readValue(jsonData, javaType);
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
System.out.println("测试字符串对象:" + objectToJson("hello world", 1));
}
}
4,运行结果
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201104101600773.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzNjIzNDQ3,size_16,color_FFFFFF,t_70#pic_center)