java递归返回json树

网上找了很多文章,很少看到有递归工具类,今天给大家写一个返回tree的json工具类,简单好用,用了jdk 8.0的方法,需要用的朋友记得安装jdk 8.0,若有更好的工具类,不吝赐教。
数据库 注意字段city_code、city_name
数据库图
工具类

/**
	/**
	 * 	生成树形json
	 * @param list 传入的数据源
	 * @param DepartmentList	接收的数据源
	 * @param city_code		字段名
	 * @param city_name字段名
	 */
	public void getTreeJson(List<LinkedHashMap<String,Object>> list,List<LinkedHashMap<String,Object>> DepartmentList,
			String city_code,String city_name) {
		if(list!=null) {
			List<LinkedHashMap<String,Object>> filterList = list.stream().filter(a -> a.get("pid")==null).collect(Collectors.toList());
			for (LinkedHashMap<String, Object> linkedHashMap : filterList) {
				LinkedHashMap<String,Object> HashMap=new LinkedHashMap<String,Object>();
				String pid = linkedHashMap.get("pid")==null?null:linkedHashMap.get("pid").toString();
				if(StringUtils.isEmpty(pid)) {
					String departmentId=linkedHashMap.get(city_code).toString();
					HashMap.put(city_code, departmentId);
					HashMap.put(city_name, linkedHashMap.get(city_name));
					List<LinkedHashMap<String,Object>> childrenList = list.stream().filter(a -> a.get("pid")!=null&&a.get("pid").toString().equals(departmentId)).collect(Collectors.toList());
					List<LinkedHashMap<String,Object>> childrenJson = getChildren(list,childrenList,city_code,city_name);
					HashMap.put("children", childrenJson);
					DepartmentList.add(HashMap);
				}
			}
		}
	}
	private List<LinkedHashMap<String,Object>> getChildren(List<LinkedHashMap<String,Object>> list,List<LinkedHashMap<String,Object>> childrenList,
			String city_code,String city_name){
		List<LinkedHashMap<String,Object>> List=new ArrayList<LinkedHashMap<String,Object>>();
		for (LinkedHashMap<String, Object> linkedHashMap : childrenList) {
			LinkedHashMap<String,Object> HashMap=new LinkedHashMap<String,Object>();
			String departmentId=linkedHashMap.get(city_code).toString();
			HashMap.put(city_code, departmentId);
			HashMap.put(city_name, linkedHashMap.get(city_name));
			List<LinkedHashMap<String,Object>> childList = list.stream().filter(a -> a.get("pid")!=null&&a.get("pid").toString().equals(departmentId)).collect(Collectors.toList());
			if(childList!=null&&childList.size()>0) {
				List<LinkedHashMap<String,Object>> drenlist = getChildren(list,childList,city_code,city_name);
				HashMap.put("children",drenlist);
			}
			List.add(HashMap);
		}
		return List;
	}

Service层调用

@Override
	public List<LinkedHashMap<String,Object>> getAdministrativeDivision() {
		List<LinkedHashMap<String,Object>> list = departmentMapper.getAdministrativeDivision();
		List<LinkedHashMap<String,Object>> DepartmentList =new ArrayList<LinkedHashMap<String,Object>>();
		if(list.size()>1) {
			userActionLog.getTreeJson(list,DepartmentList,"city_code","city_name");
			list=DepartmentList;
		}
		return list;
	}

返回json

{
	"status": 200,
	"msg": "success",
	"data": {
		"list": [{
			"city_code": "310000",
			"city_name": "上海市",
			"children": [{
				"city_code": "310100",
				"city_name": "市辖区",
				"children": [{
					"city_code": "310101",
					"city_name": "黄浦区"
				}, {
					"city_code": "310104",
					"city_name": "徐汇区"
				}, {
					"city_code": "310105",
					"city_name": "长宁区"
				}, {
					"city_code": "310106",
					"city_name": "静安区"
				}, {
					"city_code": "310107",
					"city_name": "普陀区"
				}, {
					"city_code": "310109",
					"city_name": "虹口区"
				}, {
					"city_code": "310110",
					"city_name": "杨浦区"
				}, {
					"city_code": "310112",
					"city_name": "闵行区"
				}, {
					"city_code": "310113",
					"city_name": "宝山区"
				}, {
					"city_code": "310114",
					"city_name": "嘉定区"
				}, {
					"city_code": "310115",
					"city_name": "浦东新区"
				}, {
					"city_code": "310116",
					"city_name": "金山区"
				}, {
					"city_code": "310117",
					"city_name": "松江区"
				}, {
					"city_code": "310118",
					"city_name": "青浦区"
				}, {
					"city_code": "310120",
					"city_name": "奉贤区"
				}]
			}]
		}]
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值