java后端获取数据库数据 转换为树形json

Mr_絮飞 发布于 09/20 16:47
阅读 401
收藏 1

将这张表的数据 转换为树形json 

期望获得的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": [{}]
        }
    ]
 }

 其中id 和 pid都是Integer类型  ,  谢谢各位~

加载中
0
Mr_絮飞
Mr_絮飞
		 List<HrmDepartment> departmentList = new ArrayList<HrmDepartment>();
	        // 查询所有部门
	        List<HrmDepartment> listAll = testService.queryAll();
	        for ( HrmDepartment childs: listAll ) {
	            // 子节点
	        	HrmDepartment child = childs;
	            if ( childs.getPid() == 0 ) {
	                departmentList.add(childs);
	            } else {
	                for ( HrmDepartment parents: listAll) {
	                    if ( child.getPid().equals(parents.getId()) ) {
	                        // 父节点
	                    	HrmDepartment parent = parents;
	                        // 组合父子节点的关系
	                        parent.getChildren().add(child);
	                        break;
	                    }
	                }
	            }
	        }
	        JSONArray jsonArray = (JSONArray) JSONObject.toJSON(departmentList);
	        

已经解决了~ 谢谢各位

0
realpdai
realpdai
用Node生成,然后转成json
Mr_絮飞
Mr_絮飞
我是想在java中进行转换 不知道应该如何实现
0
苏小桀

这个不难啊,加个属性children,在遍历设置这个children就ok

0
kakai
kakai

刚好我的后台菜单(权限)管理用了这个,发给你

//使用这个方法,parentId传入0即可,map参数应该是ID与对象的映射,我是用mybatis直接查询获得的map,而非list
protected final void buildJsonTree(Long parentId, ObjectNode parentJson, Map<Long, PermInfo> map) {
        //父ID对应子ID集合
        Map<Long, Set<Long>> p_c_map = new HashMap<>();
        map.forEach((Long key, PermInfo info) -> {
            Long pid = info.getParentId();
            Set<Long> children = p_c_map.get(pid);
            if (children == null) {
                children = new HashSet<>();
                p_c_map.put(pid, children);
            } else {
                children = p_c_map.get(pid);
            }
            children.add(key);
        });
        buildJsonTree(parentId, parentJson, map, p_c_map);
        p_c_map.clear();
    }

    protected final void buildJsonTree(Long parentId, ObjectNode parentJson, Map<Long, PermInfo> map, Map<Long, Set<Long>> p_c_map) {
        PermInfo parentPermInfo = map.get(parentId);
        if (parentPermInfo != null) {
            toJsonObject(parentPermInfo, parentJson);
        }
        Set<Long> childIds = p_c_map.get(parentId);
        if (childIds != null && !childIds.isEmpty()) {
            ArrayNode children = parentJson.putArray("children");
            for (Long id : childIds) {
                PermInfo permInfo = map.get(id);
                ObjectNode childNode = children.addObject();
                toJsonObject(permInfo, childNode);
                buildJsonTree(permInfo.getPermId(), childNode, map, p_c_map);
            }
        }
    }

    protected void toJsonObject(PermInfo permInfo, ObjectNode jsonObject) {
        jsonObject.put("id", permInfo.getPermId());
        jsonObject.put("name", permInfo.getName());
        jsonObject.put("pid", permInfo.getPid());
        ……
    }

 

返回顶部
顶部