weixin_42113164
野生猿
采纳率100%
2019-09-19 19:20

java中查询部门表,转换为树形json

已采纳

java中查询部门表,转换为树形json,最终需要使用Dtree 在前端展示树形结构

数据结构如下图

图片说明

不管用什么方式,最终希望得到json格式为:

{
    "id": "1",
    "name": "GM",
    "Children": [{
            "id": "22",
            "name": "PM",
            "Children": [{
                "id": "67",
                "name": "MO",
                "Children": [{}]
            }]
        },

        {
            "id": "23",
            "name": "PD",
            "Children": [{}]
        },
        {
            "id": "24",
            "name": "PE",
            "Children": [{}]
        }
    ]
 }

求大神指点

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

6条回答

  • guangcaiwudong Kevin.Y.K 2年前

    部门类

    public class Department {
    
        private String id;
    
        private String name;
    
        private String pid;
    
        public Department(String id, String name, String pid) {
            super();
            this.id = id;
            this.name = name;
            this.pid = pid;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPid() {
            return pid;
        }
    
        public void setPid(String pid) {
            this.pid = pid;
        }
    }
    

    节点类

    package com.kevin;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class TreeNode   implements Serializable{
    
        private String id;
    
        private String name;
    
        private List<TreeNode> children=new ArrayList<TreeNode>();
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public List<TreeNode> getChildren() {
            return children;
        }
    
        public void setChildren(List<TreeNode> children) {
            this.children = children;
        }
    
    
    }
    
    

    生成过程

    public static void main(String[] args) {
    
            List<Department> dList = new ArrayList<>();
    
            dList.add(new Department("1","GM","0"));
            dList.add(new Department("22","PM","1"));
            dList.add(new Department("21","R7D","1"));
            dList.add(new Department("27","STD","21"));
    
            Map<String, List<TreeNode>> map=new HashMap<String, List<TreeNode>>();
            TreeNode node=null;
            Set<String> idSet=new HashSet<>();
            for (Department department : dList) {
                if(!idSet.contains(department.getId())) {
                    node = new TreeNode();
                    node.setId(department.getId());
                    node.setName(department.getName());
    
                    if(map.get(department.getPid())==null) {
                        map.put(department.getPid(), new ArrayList<>());
                    }
    
                    map.get(department.getPid()).add(node);
    
                    idSet.add(department.getId());
                }
            }
    
            Iterator<String> it=map.keySet().iterator();
            String id=null;
            while(it.hasNext()) {
                id = it.next();
                List<TreeNode> childeNodes=map.get(id);
                for (TreeNode treeNode : childeNodes) {
                    if(map.get(treeNode.getId())!=null) {
                        treeNode.getChildren().addAll(map.get(treeNode.getId()));
                    }
                }
            }
    
            //System.out.println(map.get("0").toString());
            System.out.println(JSONObject.toJSON(map.get("0")));
    
        }
    

    测试结果

    [
        {
            "children":[
                {
                    "children":[
    
                    ],
                    "name":"PM",
                    "id":"22"
                },
                {
                    "children":[
                        {
                            "children":[
    
                            ],
                            "name":"STD",
                            "id":"27"
                        }
                    ],
                    "name":"R7D",
                    "id":"21"
                }
            ],
            "name":"GM",
            "id":"1"
        }
    ]
    

    些许不一样,按需参考

    点赞 2 评论 复制链接分享
  • qq_41080163 小菜鸟啊6 2年前

    可以用2个list回调

    点赞 评论 复制链接分享
  • qq_16207635 ontalk.club 2年前

    这是我以前写的一个树形转化工具类,可以参考下:https://blog.csdn.net/qq_16207635/article/details/101055417

    点赞 评论 复制链接分享
  • qq_19309473 Roc-xb 2年前

    首先定义一个TreeNode类,里面包含一个List

    点赞 评论 复制链接分享
  • w172087242 little_how 2年前

    可以让前端转换啊,你给他数组就行了;

    我这里写了一篇前端转换的工具,你们前端可以直接拿来用;

    平行转树形(https://blog.csdn.net/w172087242/article/details/101028111 )

    如果还有疑问可以随时发消息给我,java端的转换思想也是一样的。

    希望可以帮到你

    点赞 评论 复制链接分享
  • dmankill dmankill 2年前

    用递归就行了,结果用json库转换成json字符串

    点赞 1 评论 复制链接分享

为你推荐