Check out the data is returned to the front in a tree format Json

Copyright: respect for the original, as to reproduction, please write source. Thank https://blog.csdn.net/qq_39052982/article/details/89148518

Hello, Hello, everyone.
long time no see.

Today I brought a tree how to end is returned json. When we have a hierarchy on how to do it?
Many people may think of using this recursive way, but use recursion, then ... all too easy to fall into an infinite loop. Then fill the stack. The Ray White stepped on this. Ha ha ha.

Well, after their keep trying, and finally used a stupid way to solve this problem. Although the code to be more than the recursive so a Diudiu, but relatively simple. For white to understand. Look down ...

I just started using recursion, let's look at the code

This is manage layer

  //用List构建带有层次结构的json数据
  //List父子节点构造树形Json
  //将list集合转成树形结构的list集合
    public static List<TConMenu> listToTree(List<TConMenu> list) {
        //用递归找子。
        List<TConMenu> treeList = new ArrayList<TConMenu>();
        for (TConMenu tree : list) {
            if (tree.getName().equals(null)) {
                treeList.add(findChildren(tree, list));
            }
        }
        return treeList;
    }
    //寻找子节点
    private static TConMenu findChildren(TConMenu tree, List<TConMenu> list) {
        for (TConMenu node : list) {
            if (node.getName().equals(tree.getName())) {
                if (tree.getChildren() == null) {
                    tree.setChildren(new ArrayList<TConMenu>());
                }
                tree.getChildren().add(findChildren(node, list));
            }
        }
        return tree;
    }

Then look at the service layer

  public List<TConMenu> selectMenuTree(){
        List<TConMenu> dbMenuList = manage.selectListTree();
        return dobuildMenuTree(dbMenuList);
        //调用方法生成树形结构的List集合
        List<TConMenu> treeList = manage.listToTree(tConMenus);
        //使用fastjson对树形list件序列化转成json字符串,过滤掉属性值为null的属性
        String message = JSON.toJSONString(treeList,SerializerFeature.PrettyFormat);
        JSONObject returnData = new JSONObject();
        //重新将json字符串转成jsonObject对象,返回给前端
        returnData.put("message",JSON.parse(message));
        return returnData;
    }

This is a test service of test

 @Test
    public void selectTree(){
    	JSONObject jsonObject  = conMenuService.selectMenuTree();
    	System.out.println(jsonObject);
    }

Used above is a recursive way we look at the results

Here Insert Picture Description

These are the reported errors, currently my white technology too dishes, caught in a cycle of death, filled with stacks and made a stack overflowed. This can not blame anyone, blame yourself.

But ... I did not give up. I changed my ways, (smirking ~)

Read on. I used a stupid way,

This is the service layer

 private List<TConMenu> dobuildMenuTree(List<TConMenu> menuList) {
		//获取第一级节点数据
		List<TConMenu> oneLevelMenuList = new ArrayList<>();
		Map<String, TConMenu> oneLevelMenuMap = new HashMap<>();
		//其他节点数据
		List<TConMenu> childrenMenuList = new ArrayList<>(); 
		for (TConMenu menu : menuList) {
			if (StringUtils.isEmpty(menu.getParentId())) {
				oneLevelMenuList.add(menu);
				oneLevelMenuMap.put(menu.getId(),menu);
			} else {
				childrenMenuList.add(menu);
			}
		}

		//整理二级菜单,放到一级菜单的children节点钟
		List<TConMenu> threeLevelMenuList = new ArrayList<>();
		Map<String, TConMenu> twoLevelMenuMap = new HashMap<>();
		doGatherChildren(oneLevelMenuMap, childrenMenuList, twoLevelMenuMap,threeLevelMenuList);

		//整理三级菜单,放到二级菜单的children节点
		List<TConMenu> fourLevelMenuList = new ArrayList<>();
		Map<String, TConMenu> threeLevelMenuMap = new HashMap<>();
		doGatherChildren(twoLevelMenuMap, threeLevelMenuList, threeLevelMenuMap,fourLevelMenuList);
		return oneLevelMenuList;
	}

	private void doGatherChildren(Map<String, TConMenu> parentMenuMap, List<TConMenu> allChildrenList,
								  Map<String, TConMenu> childrenMenuMap, List<TConMenu> childrenList ) {
		for (TConMenu menu : allChildrenList) {
			String parentId = menu.getParentId();

			if (parentMenuMap.containsKey(parentId)) {
				TConMenu parentMenu = parentMenuMap.get(parentId);
				parentMenu.addChildMenu(menu);

				childrenMenuMap.put(menu.getId(),menu);
			} else {
				childrenList.add(menu);
			}
		}
	}

Methods finished, then is called.

  public List<TConMenu> selectMenuTree(){
        List<TConMenu> dbMenuList = manage.selectListTree();
        return dobuildMenuTree(dbMenuList);
    }

Then we look at the results.
Here Insert Picture Description

He came out. Things to try several times.

This last method is stupid, with a few list several map. More than the code.
But to achieve out just fine.

White is a hard, so if this article where there is something wrong. please inform.
Finally, thank you for browsing.

Guess you like

Origin blog.csdn.net/qq_39052982/article/details/89148518