码迷,mamicode.com
首页 > Web开发 > 详细

通过递归将list<Map<String,Object>>类型的数据转换为tree组件可识别的json数据

时间:2017-09-29 00:40:02      阅读:574      评论:0      收藏:0      [点我收藏+]

标签:递归

public static JSONObject getDeptTree(List<Map<String,Object>> list,String id) throws JSONException{
		JSONObject json=new JSONObject();
		JSONArray jsons=new JSONArray();//children数组
		for (Map<String, Object> map : list) {
			String id=map.get("ID")==null ? "" : map.get("ID").toString();
			String pid=map.get("PID")==null ? "" : map.get("PID").toString();
			String name=map.get("NAME")==null ? "" : map.get("NAME").toString();
			String url=map.get("URL")==null ? "" : map.get("URL").toString();
			if(pid.equals(deptid)){
				jsons.put(getDeptTree(list,id));
			}
			if(deptid.equals(id)){
				json.put("ID",id);
				json.put("PID", pid);
				json.put("name", name);
				json.put("url",url);
			}
		}
		json.put("children", jsons);
		return json;
	}


本文出自 “随笔” 博客,请务必保留此出处http://12532790.blog.51cto.com/12522790/1969499

通过递归将list<Map<String,Object>>类型的数据转换为tree组件可识别的json数据

标签:递归

原文地址:http://12532790.blog.51cto.com/12522790/1969499

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!