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