java生成Json树形结构数据

java生成Json树形结构数据,第1张

废话少说直接上代码

    @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": []
          }
        ]
      }
    ]
  }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/877580.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存