JavaJson转Json树格式
1. 工具类
package com.tl.visual.util;
import java.util.ArrayList;
import java.util.List;
public class BuildTree {
public static <T> Tree<T> build(List<Tree<T>> nodes) {
if(nodes == null){
return null;
}
List<Tree<T>> topNodes = new ArrayList<Tree<T>>();
for (Tree<T> children : nodes) {
String pid = children.getParentId();
if (pid == null || "".equals(pid)) {
topNodes.add(children);
continue;
}
for (Tree<T> parent : nodes) {
String id = parent.getId();
if (id != null && id.equals(pid)) {
parent.getChildren().add(children);
continue;
}
}
}
Tree<T> root = new Tree<T>();
if (topNodes.size() == 1) {
root = topNodes.get(0);
} else {
root.setId("-1");
root.setParentId("");
root.setChildren(topNodes);
}
return root;
}
}
package com.tl.visual.util;
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import java.util.List;
public class Tree<T> {
private String id;
private String state;
private List<Tree<T>> children = new ArrayList<Tree<T>>();
private String parentId;
private String label;
private String create_id;
private String gmt_create;
private String modified_id;
private String gmt_modified;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<Tree<T>> getChildren() {
return children;
}
public void setChildren(List<Tree<T>> children) {
this.children = children;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getCreate_id() {
return create_id;
}
public void setCreate_id(String create_id) {
this.create_id = create_id;
}
public String getGmt_create() {
return gmt_create;
}
public void setGmt_create(String gmt_create) {
this.gmt_create = gmt_create;
}
public String getModified_id() {
return modified_id;
}
public void setModified_id(String modified_id) {
this.modified_id = modified_id;
}
public String getGmt_modified() {
return gmt_modified;
}
public void setGmt_modified(String gmt_modified) {
this.gmt_modified = gmt_modified;
}
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Tree(String id, String state, List<Tree<T>> children, String parentId, String label, String create_id, String gmt_create, String modified_id, String gmt_modified) {
this.id = id;
this.state = state;
this.children = children;
this.parentId = parentId;
this.label = label;
this.create_id = create_id;
this.gmt_create = gmt_create;
this.modified_id = modified_id;
this.gmt_modified = gmt_modified;
}
public Tree() {
super();
}
@Override
public String toString() {
return JSON.toJSONString(this);
}
private JSONResponse getAll(){
List<Tree<SystemRelate>> trees = new ArrayList<Tree<SystemRelate>>();
List<SystemRelate> systemRelates = iSystemRelateService.queryAll();
for(SystemRelate systemRelates1 : systemRelates){
Tree<SystemRelate> tree = new Tree<SystemRelate>();
tree.setId(systemRelates1.getClass_id());
tree.setParentId(systemRelates1.getParent_id());
tree.setState(systemRelates1.getState());
tree.setLabel(systemRelates1.getClass_name());
tree.setCreate_id(systemRelates1.getCreate_id());
tree.setGmt_create(systemRelates1.getGmt_create().toString());
if(systemRelates1.getGmt_modified()!=null){
tree.setGmt_modified(systemRelates1.getGmt_modified().toString());
}
trees.add(tree);
}
Tree<SystemRelate> t = BuildTree.build(trees);
return ResultUtil.success("",t);
}