数据库设计:
转化成json之后是这样的
现在想把json封装成固定的格式 看了网上了许多贴子后 出来的效果有点不满意 
请问该如何处理? 麻烦大佬们帮帮忙

解决方案 »

  1.   

    我做得一般都是自己拼接的,遍历json,然后根据父id查询,查询出的子就装到children里面,出来差不多是这样的{
        "id": "",
        "name": "",
        "value": "",
        "parentId": "",
        "children": [
            {
                "id": "",
                "name": "",
                "value": "",
                "parentId": "",
                "children": [
                    {
                        "id": "",
                        "name": "",
                        "value": "",
                        "parentId": "",
                        "children": [
                            {}
                        ]
                    }
                ]
            }
        ]
    }
      

  2.   

    对  就是这个效果,大佬怎么做的,烦请不吝赐教 非常感谢
    我写了一个测试方法,你参考下:@Test
        public void test() {
            String str = "[{\"id\": \"1\",\"name\": \"1\",\"value\": \"1\",\"parentid\": \"0\"},{\"id\": \"2\",\"name\": \"2\",\"value\": \"2\",\"parentid\": \"0\"},{\"id\": \"11\",\"name\": \"11\",\"value\": \"11\",\"parentid\": \"1\"},{\"id\": \"111\",\"name\": \"111\",\"value\": \"111\"," +
                    "\"parentid\": \"11\"},{\"id\": \"12\",\"name\": \"12\",\"value\": \"12\",\"parentid\": \"1\"},{\"id\": \"22\",\"name\": \"22\",\"value\": \"22\",\"parentid\": \"2\"}]";
            JSONArray jsonArray = JSONObject.parseArray(str);
            //取到第一位
            JSONObject result = new JSONObject(new LinkedHashMap());
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject obj = jsonArray.getJSONObject(i);
                if (result.size() == 0 && obj.getString("parentid").equals("0")) {
                    result.putAll(obj);
                }
            }
            //遍历下面的子集合
            children(result, result.getString("id"), jsonArray);
            System.out.println(result.toJSONString());
        }    private JSONObject children(JSONObject result, String parentId, JSONArray jsonArray) {
            JSONArray resultArr = new JSONArray();//子集合
            for (int j = 0; j < jsonArray.size(); j++) {
                JSONObject jobj = jsonArray.getJSONObject(j);
                if (parentId.equals(jobj.getString("parentid"))) {
                    resultArr.add(jobj);
                    children(jobj, jobj.getString("id"), jsonArray);
                }
            }
            result.put("children", resultArr);
            return result;
        }
      

  3.   

    /**
     * Date: 2019/2/22
     * Time: 11:01
     * 树工具类
     * @author joefan
     * @version 1.0
     */
    public class TreeUtil {    /**
         * 组件转化为树形节点并遍历
         *
         * @param list
         */
        public static <T> List<TreeNode> traverseAsync(List<T> list) {
            List<TreeNode> treeNodes = list.stream().map(TreeNode::fromObject).collect(Collectors.toList());
            List<TreeNode> removeList = new ArrayList<>();
            List<TreeNode> returnList = treeNodes.stream()
                    .map(root -> traverseAsync(root, treeNodes, removeList))
                    .collect(Collectors.toList());
            returnList.removeAll(removeList);
            return returnList;
        }    /**
         * 组件遍历所有树形节点.
         *
         * @param root  根节点
         * @param nodes 所有节点
         * @return 树节点
         */
        private static TreeNode traverseAsync(TreeNode root, List<TreeNode> nodes, List<TreeNode> removeList) {
            List<TreeNode> children = root.getChildren(nodes);
            if (children == null || children.size() == 0) {
                root.setLeaf(true);
            }
            removeList.addAll(children);
            root.setChildren(children);
            children.forEach(child -> traverseAsync(child, nodes, removeList));
            return root;
        }
    }
    /**
     * 树节点
     * Date: 2019/2/20
     * Time: 14:32
     *
     * @author joefan
     * @version 1.0
     **/
    @Data
    public class TreeNode {
        private String key;
        private String parent;
        private String title;
        private List<TreeNode> children;
        private boolean isLeaf;
        private Boolean checked;
        private Object extend;//扩展字段    /**
         * 生成树节点
         * @param object 实体对象
         */
        public static TreeNode fromObject(Object object){
            TreeNode treeNode = new TreeNode();
            treeNode.extend = object;
            treeNode.isLeaf = false;
            treeNode.setChecked(false);
            return otherFiled(treeNode, object);
        }    private static TreeNode otherFiled(TreeNode treeNode, Object object) {
            if (object instanceof SysArea) {
                treeNode.key = getValue(object,"id");
                treeNode.title = getValue(object,"areaName");
                treeNode.parent = getValue(object, "parentId");
            }else  {
                treeNode.key = getValue(object,"id");
                treeNode.title = getValue(object,"centerName");
                treeNode.parent = getValue(object, "parentId");
            }
            return treeNode;
        }    /**
         * 获取子节点
         * @description 
         * @param nodes all nodes
         * @return node list
         */
        public List<TreeNode> getChildren(List<TreeNode> nodes) {
            return nodes.stream().filter(node -> Objects.equals(key, node.getParent()))
                    .collect(Collectors.toList());
        }}package com.honggv.xueliang.util;import java.lang.reflect.Field;/**
     * 反射工具类
     * @Author: joefan
     * @Date: 2019/2/27 14:40
     */
    class ReflectionUtils {    /**
         * 根据反射取值
         * @param object 实体对象
         * @param fieldName 属性名称
         * @return String
         */
        static String getValue(Object object, String fieldName) {
            Field field = org.springframework.util.ReflectionUtils.findField(object.getClass(), fieldName);
            if (field == null)
                return null;
            try {
                field.setAccessible(true);
                return String.valueOf(field.get(object).toString());
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }    /**
         * 根据反射取值
         * @param object 实体对象
         * @param fieldName 属性名称
         * @return Object
         */
        static Object getValueObject(Object object, String fieldName) {
            Field field = org.springframework.util.ReflectionUtils.findField(object.getClass(), fieldName);
            if (field == null)
                return null;
            try {
                field.setAccessible(true);
                return field.get(object);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }    /**
         * 根据反射取值
         * @param object 实体对象
         * @param fieldName 属性名称
         * @return Boolean
         */
        static Boolean getValueBoolean(Object object, String fieldName) {
            Field field = org.springframework.util.ReflectionUtils.findField(object.getClass(), fieldName);
            if (field == null)
                return null;
            try {
                field.setAccessible(true);
                return field.get(object) != null && Boolean.valueOf(field.get(object).toString());
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }