Java递归生成树形菜单结构的json

Java递归生成树形菜单结构的json

  • 1.数据class
  • 2.获取数据方法
  • 3. 组装数据的的递归方法
  • 4. 案例
  • 5. 结果输出

1.数据class

Dept.java

public class Dept {
    /** uuid */
    private String id;
    /** 部门名称 */
    private String  name;
    /** 父id */
    private String parentId;

    private List<MfjcDeptVo> children;

    public MfjcDeptVo(String id, String name, String parentId) {
        this.id = id;
        this.name = name;
        this.parentId = parentId;
   	}
}

2.获取数据方法

private List<Dept> getListDept(){
    List<Dept> deptList = new ArrayList<>();
    deptList.add(new Dept("001","一级部门1","0"));
    deptList.add(new Dept("009","一级部门2","0"));
    deptList.add(new Dept("002","二级部门1","001"));
    deptList.add(new Dept("003","三级部门1","002"));
    deptList.add(new Dept("004","二级部门2","001"));
    deptList.add(new Dept("005","三级部门2","002"));
    deptList.add(new Dept("006","二级部门3","001"));
    deptList.add(new Dept("007","三级部门3","002"));
    deptList.add(new Dept("008","三级部门4","002"));
	return deptList;
}

3. 组装数据的的递归方法

private List<Dept> deepCategory(String id, List<Dept> deptList){
        List<Dept> result = new ArrayList<>();
        deptList.forEach(x ->{
            if (id.equals(x.getParentId())){
                List<Dept> dept = this.deepCategory(x.getId(), deptList);
                x.setChildren(dept);
                result.add(x);
            }
        });
        return result;
}

4. 案例

public String getDept(){
	List<Dept> deptList = getListDept();
	List<Dept> deptListTree = deepCategory("0", deptList);
	String jsonString = JSONObject.toJSONString(deptListTree);
	System.out.println(jsonString); //输出结果请看第5部分
	return jsonString;
}

private List<Dept> getListDept(){
    List<Dept> deptList = new ArrayList<>();
    deptList.add(new Dept("001","一级部门1","0"));
    deptList.add(new Dept("009","一级部门2","0"));
    deptList.add(new Dept("002","二级部门1","001"));
    deptList.add(new Dept("003","三级部门1","002"));
    deptList.add(new Dept("004","二级部门2","001"));
    deptList.add(new Dept("005","三级部门2","002"));
    deptList.add(new Dept("006","二级部门3","001"));
    deptList.add(new Dept("007","三级部门3","002"));
    deptList.add(new Dept("008","三级部门4","002"));
	return deptList;
}

private List<Dept> deepCategory(String id, List<Dept> deptList){
        List<Dept> result = new ArrayList<>();
        deptList.forEach(x ->{
            if (id.equals(x.getParentId())){
                List<Dept> dept = this.deepCategory(x.getId(), deptList);
                x.setChildren(dept);
                result.add(x);
            }
        });
        return result;
}

5. 结果输出

[
	{
		"children": [
			{
				"children": [
					{
						"children": [],
						"id": "003",
						"name": "三级部门1",
						"parentId": "002"
					},
					{
						"children": [],
						"id": "005",
						"name": "三级部门2",
						"parentId": "002"
					},
					{
						"children": [],
						"id": "007",
						"name": "三级部门3",
						"parentId": "002"
					},
					{
						"children": [],
						"id": "008",
						"name": "三级部门4",
						"parentId": "002"
					}
				],
				"id": "002",
				"name": "二级部门1",
				"parentId": "001"
			},
			{
				"children": [],
				"id": "004",
				"name": "二级部门2",
				"parentId": "001"
			},
			{
				"children": [],
				"id": "006",
				"name": "二级部门3",
				"parentId": "001"
			}
		],
		"id": "001",
		"name": "一级部门1",
		"parentId": "0"
	},
	{
		"children": [],
		"id": "009",
		"name": "一级部门2",
		"parentId": "0"
	}
]
上一篇:如何在Vue3中使用H.265视频EasyPlayer.js流媒体播放器?


下一篇:Android 性能优化之黑科技开道(二)