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


    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;
       	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    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"
    	}
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
  • 相关阅读:
    RabbitMQ学习总结-延迟消息
    使用i18n Ayll在项目中也能显示中文
    智慧校园管理在疫情防控中的作用有哪些?
    netty学习
    Kubernetes资源编排系列之三: Kustomize篇
    Java网关的统一异常处理
    Android 项目Gradle文件讲解(Groovy和Kotlin)
    HJ86 求最大连续bit数
    主从模式详解
    LeetCode75-06:移动零
  • 原文地址:https://blog.csdn.net/qq_27480007/article/details/138176097