java后台管理生成json递归转换数据。

Java开发千人交流群 677807540   大家可以一起讨论技术欢迎加入

1.我们管理系统菜单需要得json

var data = [{
	id: 1,
	name: "病案质量",
	icon: "me glyphicon glyphicon-home",
	children: [
		{
			name: "科室质控",
			href: "../deptqua/table",
			icon: "glyphicon glyphicon-list-alt"
		},
		{
			name: "科室维度分析",
			href: "../deptdimension/table",
			icon: "glyphicon glyphicon-list-alt"
		},
		{
		name: "科室服务能力",
		href: "../performance/departmentContrast",
		icon: "glyphicon glyphicon-list-alt"
	    },
	    {
		name: "科室服务效率",
		href: "../performance/depContrastTimeCost",
		icon: "glyphicon glyphicon-list-alt"
	}]

}];

2.查出所有菜单数据,可以区分主菜单,和子菜单就可以了,下面进行递归转成json(controller代码)。

/*
	 * 获取菜单数据递归整理数据json 程志恒
	 * 
	 */
	@RequestMapping(value = "/cdList")
	public void getCd(HttpServletResponse response) {
		// 获取登录信息
		ZyhdShiroUser user = getCurrentUser();

		Map map = new HashMap();
		// 获取当前登录得用户,查询权限菜单
		map.put("userName", user.getUserCode());

		// 根据用户名查询菜单权限一级菜单
		List<SimpleTreeModel> ListCd = loginCdService.getCd(map);
		// 递归创建菜单json
		List<SimpleTreeModel> simpleTreeModel = SimpleTreeModel.CreateCd(ListCd);
		String countyJson = JsonUtilsService.bean2JsonString(simpleTreeModel);
		AjaxUtils.rendText(response, countyJson);
	}

2.实体类以及所封装得递归方法。

package com.msunsoft.drgs.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;





/**
 * 单根,简单树结构,用于提供easyui-tree,combotree的数据源
 * @author Administrator
 *
 */
public class SimpleTreeModel {

	private String id;
	private String name;
	private String href;
	private String icon;
	private String parentId;
	private String pId;
	

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public String getpId() {
		return pId;
	}

	public void setpId(String pId) {
		this.pId = pId;
	}




	private List<SimpleTreeModel> children;
	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}
	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 getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	

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

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

	

	/**
	 * 创建递归结构的单根简单树
	 * @param map
	 * @param rootId
	 * @return
	 */
	public static SimpleTreeModel CreateFromListHashMap(List<HashMap<String, Object>> map,String rootId){
		SimpleTreeModel root = new SimpleTreeModel();
		int rootIndex = -1;
		//获取根节点
		for(int i=0,length = map.size();i<length;i++){
			if(map.get(i).get("id").equals(rootId)){
				rootIndex = i;
				break;
			}
		}
		//获取子节点
		List<SimpleTreeModel> children = new ArrayList<SimpleTreeModel>();

		for(int i=0,length=map.size();i<length;i++){
			if(map.get(i).get("parentid").equals(rootId)){
				children.add(SimpleTreeModel.CreateFromListHashMap(map, (String)map.get(i).get("id")));
			}
		}
		if(rootIndex!=-1){
			root.setId((String)map.get(rootIndex).get("ID"));
			root.setName((String)map.get(rootIndex).get("NAME"));
			root.setIcon((String)map.get(rootIndex).get("ICON"));
		
		}
	
		
		root.setChildren(children);
		
		return root;
	}
	

	
	
	//创建菜单树结构
	public static  List<SimpleTreeModel> CreateCd(List<SimpleTreeModel> ListCd){
		 List<SimpleTreeModel> menuList = new ArrayList<SimpleTreeModel>(); // 树递归
			// 先找到所有的一级菜单
			    for (int i = 0; i < ListCd.size(); i++) {
			        // 一级菜单父ID为0
			        if (ListCd.get(i).getParentId().equals("1")) {
			            menuList.add(ListCd.get(i));
			        }
			    }
			    // 为一级菜单设置子菜单,getChild是递归调用的
			    for (SimpleTreeModel menu : menuList) {
			        menu.setChildren(getChild(menu.getId(), ListCd));
			    }
        return menuList;
	}
	
	/**
	 * 子菜单递归
	 * @param id
	 * @param rootMenu
	 * @return
	 */
	private static List<SimpleTreeModel> getChild(String id, List<SimpleTreeModel> rootMenu) {
	    // 子菜单
	    List<SimpleTreeModel> childList = new ArrayList<>();
	    for (SimpleTreeModel menu : rootMenu) {
	        // 遍历所有节点,将父菜单id与传过来的id比较
            if (menu.getParentId().equals(id)) {
                childList.add(menu);
            }
	    }
	    
	    // 把子菜单的子菜单再循环一遍
		/*
		 * for (SimpleTreeModel menu : childList) {
		 * menu.setChildren(getChild(menu.getId(), rootMenu));// 递归 }
		 */
	    
	    // 判断递归结束
	    if (childList.size() == 0) {
	        return null;
	    }
	    return childList;
	}
	

}













   3.转换json字符串

/**
	 * 对象转换为JSON格式字符串
	 * @param obj
	 * @return
	 */
	public static String bean2JsonString(Object obj){
		if(null != obj){
			return JSONArray.fromObject(obj).toString();
		}
		return null;
	}

4.返回数据

/**
	 * 输出字符串到前台页面
	 * 
	 * @param response
	 * @param content
	 * @throws Exception
	 */
	public static void rendText(HttpServletResponse response, String content) {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.write(content);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (null != out) {
				out.close();
			}
		}
	}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值