java 返回树结构
RegionBeanTree beanTree2 = new RegionBeanTree();
beanTree2.setCode("2");
beanTree2.setLable("端口开放异常");
beanTree2.setPid("0");
RegionBeanTree beanTree3 = new RegionBeanTree();
beanTree3.setCode("3");
beanTree3.setLable("进程数量异常");
beanTree3.setPid("0");
RegionBeanTree beanTree4 = new RegionBeanTree();
beanTree4.setCode("4");
beanTree4.setLable("进程名称异常");
beanTree4.setPid("0");
RegionBeanTree beanTree5 = new RegionBeanTree();
beanTree5.setCode("5");
beanTree5.setLable("其他");
beanTree5.setPid("0");
RegionBeanTree beanTree51 = new RegionBeanTree();
beanTree51.setCode("51");
beanTree51.setLable("其他1");
beanTree51.setPid("5");
List<RegionBeanTree> childrenNodeList = new ArrayList<>();
childrenNodeList.add(beanTree51);
beanTree5.setChildren(childrenNodeList);
List<RegionBeanTree> rootNodeList = new ArrayList<>();
rootNodeList.add(beanTree2);
rootNodeList.add(beanTree3);
rootNodeList.add(beanTree4);
rootNodeList.add(beanTree5);
System.out.println(JsonUtils.toJson(rootNodeList));
第二种
RegionBeanTree beanTree2 = new RegionBeanTree();
beanTree2.setCode("2");
beanTree2.setLable("端口开放异常");
beanTree2.setPid("0");
RegionBeanTree beanTree3 = new RegionBeanTree();
beanTree3.setCode("3");
beanTree3.setLable("进程数量异常");
beanTree3.setPid("0");
RegionBeanTree beanTree4 = new RegionBeanTree();
beanTree4.setCode("4");
beanTree4.setLable("进程名称异常");
beanTree4.setPid("0");
RegionBeanTree beanTree5 = new RegionBeanTree();
beanTree5.setCode("5");
beanTree5.setLable("其他");
beanTree5.setPid("0");
RegionBeanTree beanTree51 = new RegionBeanTree();
beanTree5.setCode("51");
beanTree5.setLable("其他1");
beanTree5.setPid("5");
RegionBeanTree beanTree6 = new RegionBeanTree();
List<RegionBeanTree> rootList = new ArrayList<>();
rootList.add(beanTree2);
rootList.add(beanTree3);
rootList.add(beanTree4);
rootList.add(beanTree5);
List<RegionBeanTree> bodyList = new ArrayList<>();
bodyList.add(beanTree2);
bodyList.add(beanTree3);
bodyList.add(beanTree4);
bodyList.add(beanTree5);
for (int i = 0; i < list.size(); i++) {
Map<String, String> map = list.get(i);
if("1".equals(map.get("type"))){
RegionBeanTree beanTemp = new RegionBeanTree();
beanTemp.setCode(map.get("ruleId"));
beanTemp.setLable(map.get("ruleName"));
beanTemp.setPid("1");
bodyList.add(beanTemp);
}else if("2".equals(map.get("type"))){
RegionBeanTree beanTemp = new RegionBeanTree();
beanTemp.setCode(map.get("ruleId"));
beanTemp.setLable(map.get("ruleName"));
beanTemp.setPid("2");
bodyList.add(beanTemp);
}else if("3".equals(map.get("type"))){
RegionBeanTree beanTemp = new RegionBeanTree();
beanTemp.setCode(map.get("ruleId"));
beanTemp.setLable(map.get("ruleName"));
beanTemp.setPid("3");
bodyList.add(beanTemp);
}else if("4".equals(map.get("type"))){
RegionBeanTree beanTemp = new RegionBeanTree();
beanTemp.setCode(map.get("ruleId"));
beanTemp.setLable(map.get("ruleName"));
beanTemp.setPid("4");
bodyList.add(beanTemp);
}
}
TreeBuilder utils = new TreeBuilder(rootList, bodyList);
return utils.getTree();
tree类
public class RegionBeanTree {
private String code;
private String lable;
private String pid;
private List<RegionBeanTree> children;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getLable() {
return lable;
}
public void setLable(String lable) {
this.lable = lable;
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public List<RegionBeanTree> getChildren() {
return children;
}
public void setChildren(List<RegionBeanTree> children) {
this.children = children;
}
}