java生成json无级菜单树

利用java生成json无级菜单树

最近做毕业设计时要用到前后端分离,利用json传递数据,而多级菜单数据需要以json形式返回给前端页面,因此记录一下相关的方法 ,废话不多说,直接上代码。

package cn.xyes.entity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Tireless
 */
public class MenuTree {
private List<Menu> menuList = new ArrayList<Menu>();
public MenuTree(List<Menu> menuList) {
    this.menuList=menuList;
}

//建立树形结构
public List<Menu> builTree(){
    List<Menu> treeMenus =new  ArrayList<Menu>();
    for(Menu menuNode : getRootNode()) {
        menuNode=buildChilTree(menuNode);
        treeMenus.add(menuNode);
    }
    return treeMenus;
}

//递归,建立子树形结构
private Menu buildChilTree(Menu pNode){
    List<Menu> chilMenus =new  ArrayList<Menu>();
    for(Menu menuNode : menuList) {
        if(menuNode.getParent_id().equals(pNode.getId())) {
            chilMenus.add(buildChilTree(menuNode));
        }
    }
    pNode.setChildren(chilMenus);
    return pNode;
}

//获取根节点
private List<Menu> getRootNode() {
    List<Menu> rootMenuLists =new  ArrayList<Menu>();
    for(Menu menuNode : menuList) {
        if(menuNode.getParent_id().equals("0")) {
            rootMenuLists.add(menuNode);
        }
    }
    return rootMenuLists;
  }
}
package cn.xyes.entity;

import java.util.List;

/**
 * @author Tireless
 */
public class Menu {
//菜单id
private String id; 
//菜单名称
private String name;
//父菜单id
private String parent_id;
private List <Menu> children;

public Menu(String id, String name, String parent_id) {
    this.id = id;
    this.name = name;
    this.parent_id = parent_id;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getParent_id() {
    return parent_id;
}

public void setParent_id(String parent_id) {
    this.parent_id = parent_id;
}

public List<Menu> getChildren() {
    return children;
}

public void setChildren(List<Menu> children) {
    this.children = children;
}

@Override
public String toString() {
    return "Menu{" +
            "id='" + id + '\'' +
            ", name='" + name + '\'' +
            ", parent_id='" + parent_id + '\'' +
            ", children=" + children +
            '}';
    }
  }

测试代码

 /**
 *json返回数据
 **/
@RequestMapping("/demo")
@ResponseBody
public JsonResult jsonResult(){
    List<Menu> menu = new ArrayList<>();
  //实际开发中可以使用数据库中查询到的数据替换下面数据
    menu.add(new Menu("1","一级菜单1","0"));
    menu.add(new Menu("2","一级菜单2","0"));
    menu.add(new Menu("3","二级菜单1","1"));
    menu.add(new Menu("4","二级菜单2","1"));
    menu.add(new Menu("5","三级菜单1","3"));

    // 先找到所有的一级菜单
    MenuTree menuTree =new MenuTree(menu);
    menu=menuTree.builTree();
    return new JsonResult(menu,StatusCode.SUCCESS);
}

}

返回json数据样式

[
{
    "id": "1",
    "name": "一级菜单1",
    "parent_id": "0",
    "children": [
        {
            "id": "3",
            "name": "二级菜单1",
            "parent_id": "1",
            "children": [
                {
                    "id": "5",
                    "name": "三级菜单1",
                    "parent_id": "3",
                    "children": []
                }
            ]
        },
        {
            "id": "4",
            "name": "二级菜单2",
            "parent_id": "1",
            "children": []
        }
    ]
},
 {
    "id": "2",
    "name": "一级菜单2",
    "parent_id": "0",
    "children": []
 }
]

这里就不再贴效果图了,懂得都懂。

posted @ 2021-11-23 18:34  Tireless  阅读(331)  评论(0编辑  收藏  举报