java使用递归将查询的数据转JSON树状结构数据

java使用递归将查询的数据转JSON树状结构数据

1.第一种情况,从数据库中查出数据如下:
java使用递归将查询的数据转JSON树状结构数据_第1张图片
使用以下代码进行转换,先提取pId为0的父节点,再把子节点递归提取出来

`public static List<Map> toTreeByMap(List<Map> list) {
     
        List<Map> treeList = new ArrayList<>();
        for (Map tree : list) {
     
            if ("0".equals(tree.get("pId"))) {
     
                treeList.add(tree);
            }
        }
        for (Map tree : list) {
     
            toTreeChildren(treeList, tree);
        }
        return treeList;
    }
    
    private static void toTreeChildren(List<Map> treeList, Map tree) {
     
        for (Map node : treeList) {
     
            if (tree.get("pId").equals(node.get("id"))) {
     
                if (node.get("children") == null || "".equals(node.get("children"))) {
     
                    node.put("children", new ArrayList<Map>());
                }
                ((ArrayList<Map>) node.get("children")).add(tree);
            }
            if (node.get("children") != null) {
     
                toTreeChildren((ArrayList<Map>) node.get("children"), tree);
            }
        }
    }`

转换完的数据如下:

{
     
  "code": 1,
  "message": "成功",
  "result": {
     
    "id": "0",
    "pId": "-1",
    "funcode": "sysmd",
    "funname": "系统功能菜单",
    "checked": 0,
    "children": [
      {
     
        "funcode": "sysmd1",
        "checked": 1,
        "pId": "0",
        "id": "a0980baf639679b4016396928fbb0001",
        "funname": "首页"
      },
      {
     
        "funcode": "sysmd2",
        "checked": 1,
        "pId": "0",
        "id": "a0980baf639679b4016396928fbb0002",
        "funname": "首页"
      },
      {
     
        "funcode": "sysmd3",
        "checked": 1,
        "pId": "0",
        "id": "a0980baf639679b4016396928fbb0003",
        "funname": "监控告警",
        "children": [
          {
     
            "funcode": "sysmd31",
            "checked": 1,
            "pId": "a0980baf639679b4016396928fbb0003",
            "id": "a0980baf639679b4016396928fbb0008",
            "funname": "设备告警"
          },
          {
     
            "funcode": "sysmd32",
            "checked": 1,
            "pId": "a0980baf639679b4016396928fbb0003",
            "id": "a0980baf639679b4016396928fbb0009",
            "funname": "污染告警"
          }
        ]
      }
    ]
  }
}

2.第二种情况,前端传过来的JSON数据转成List集合:

[
  {
     
    "id": "0",
    "pId": "-1",
    "funcode": "sysmd",
    "funname": "系统功能菜单",
    "checked": true,
    "children": [
      {
     
        "funcode": "sysmd1",
        "checked": true,
        "pId": "0",
        "id": "a0980baf639679b4016396928fbb0001",
        "funname": "首页"
      },
      {
     
        "funcode": "sysmd2",
        "checked": true,
        "pId": "0",
        "id": "a0980baf639679b4016396928fbb0002",
        "funname": "首页"
      },
      {
     
        "funcode": "sysmd3",
        "checked": true,
        "pId": "0",
        "id": "a0980baf639679b4016396928fbb0003",
        "funname": "监控告警",
        "children": [
          {
     
            "funcode": "sysmd31",
            "checked": true,
            "pId": "a0980baf639679b4016396928fbb0003",
            "id": "a0980baf639679b4016396928fbb0008",
            "funname": "设备告警"
          },
          {
     
            "funcode": "sysmd32",
            "checked": true,
            "pId": "a0980baf639679b4016396928fbb0003",
            "id": "a0980baf639679b4016396928fbb0009",
            "funname": "污染告警"
          }
        ]
      }
    ]
  }
]

后端使用String接收,然后序列化为List类型:

public Object setUserFunctionPermission(@RequestParam("list") String permfunctionmaplistjson){
     
        ObjectMapper mapper = new ObjectMapper(); // 使用ObjectMapper反序列化json串为对象
        List<Map> permfunctionmaplist = new ArrayList<>();
        permfunctionmaplist = mapper.readValue(permfunctionmaplistjson, permfunctionmaplist.getClass());
        //用于保存转换后的集合
        List<Map> treeNode = new ArrayList<>();
        //使用对象工具类中的方法进行递归转换
        getTreeChildren(permfunctionmaplist, treeNode);
        return treeNode;
}
	//提取所有子节点
    public static void getTreeChildren(List<Map> treeList, List<Map> lm) {
     
        for (Map tree : treeList) {
     
            if (tree.get("children") != null && !"".equals(tree.get("children"))) {
     
                getTreeChildren((ArrayList<Map>) tree.get("children"), lm);
            } else {
     
                lm.add(tree);
            }
        }
    }

提取出来的后treeNode数据为:

[{funcode=sysmd1,checked=1,pId=0,id=a0980baf639679b4016396928fbb0001,
funname=首页},
{funcode=sysmd2,checked=1,pId=0,id=a0980baf639679b4016396928fbb0002,
funname=首页},
{funcode=sysmd31,checked=1,pId=a0980baf639679b4016396928fbb0003,id=a0980baf639679b4016396928fbb0008, funname=设备告警},
{funcode=sysmd32,checked=1,pId=a0980baf639679b4016396928fbb0003,id=a0980baf639679b4016396928fbb0009, funname=污染告警}]

你可能感兴趣的:(通用工具类,java,json)