java生成Json树形结构数据

废话少说直接上代码

    @Override
    public List<Subject> getAllSubject() {
        //    1    查询所有分类
        List<Subject> entities = baseMapper.selectList(null);


//        2    组装成父子的树形结构
//            2.1 找到所有的一级分类
        //获取父节点
        List<Subject> collect = list.stream().filter(m ->
                m.getParentId().equals("0")
        ).map(
                (m) -> {
                    m.setChildren(getChildrens(m, list));
                    return m;
                }
        ).collect(Collectors.toList());
        return collect;
    }
    /**
     * 递归查询子节点
     * @param root  根节点
     * @param all   所有节点
     * @return 根节点信息
     */
    private List<Subject> getChildrens(Subject root, List<Subject> all) {
        List<Subject> children = all.stream().filter(m -> {
            return Objects.equals(m.getParentId(), root.getId());
        }).map(
                (m) -> {
                    m.setChildren(getChildrens(m, all));
                    return m;
                }
        ).collect(Collectors.toList());
        return children;
    }

实体类:

@Data
public class Subject {
    private String id ;
    private String title ;
    private String parentId;
    private List<Subject> children ;
}

效果:

{
  "success": true,
  "code": 20000,
  "message": "成功",
  "data": {
    "list": [
      {
        "id": "1522595690457456641",
        "title": "前端开发",
        "parentId": "0",
        "children": [
          {
            "id": "1522595690482622465",
            "title": "vue",
            "parentId": "1522595690457456641",
            "children": []
          },
          {
            "id": "1522595690516176897",
            "title": "JavaScript",
            "parentId": "1522595690457456641",
            "children": []
          },
          {
            "id": "1522595690541342722",
            "title": "Jquery",
            "parentId": "1522595690457456641",
            "children": []
          },
          {
            "id": "1522595690725892097",
            "title": "bootstrap",
            "parentId": "1522595690457456641",
            "children": []
          }
        ]
      },
      {
        "id": "1522595690562314241",
        "title": "后端开发",
        "parentId": "0",
        "children": [
          {
            "id": "1522595690579091458",
            "title": "Java",
            "parentId": "1522595690562314241",
            "children": []
          },
          {
            "id": "1522595690608451586",
            "title": "C#",
            "parentId": "1522595690562314241",
            "children": []
          }
        ]
      },
      {
        "id": "1522595690625228802",
        "title": "数据库开发",
        "parentId": "0",
        "children": [
          {
            "id": "1522595690642006017",
            "title": "mysql",
            "parentId": "1522595690625228802",
            "children": []
          },
          {
            "id": "1522595690675560450",
            "title": "redis",
            "parentId": "1522595690625228802",
            "children": []
          },
          {
            "id": "1522595690700726274",
            "title": "MongoDB",
            "parentId": "1522595690625228802",
            "children": []
          }
        ]
      }
    ]
  }
}

以上叶子节点会有空集合地址

附件补充:
有时候不想要空节点也出现地址在这里插入图片描述
而是要为null
代码附上:

@Override
public List queryTreeNodes() {

List categoryList = courseCategoryMapper.selectList(null);

List categoryDTos = new ArrayList<>();
List categoryTreeDToList = new ArrayList<>();

for (CourseCategory courseCategory : categoryList) {
    CourseCategoryTreeDTo categoryTreeDTo = new CourseCategoryTreeDTo();
    BeanUtils.copyProperties(courseCategory, categoryTreeDTo);
    categoryDTos.add(categoryTreeDTo);
}
HashMap nodeMap = new HashMap<>() ;

categoryDTos.stream().forEach(item->{
    nodeMap.put(item.getId(),item);
    if(item.getParentid().equals("1")){
        categoryTreeDToList.add(item);
    }

    String parentid = item.getParentid() ;

    CourseCategoryTreeDTo parentNode = nodeMap.get(parentid);

    if(parentNode!=null){
        List childrenTreeNodes = parentNode.getChildrenTreeNodes();
        if(childrenTreeNodes == null){
            parentNode.setChildrenTreeNodes(new ArrayList());
        }
        parentNode.getChildrenTreeNodes().add(item) ;
    }
});

return categoryTreeDToList ;

}

你可能感兴趣的:(java,json)