关于json树的处理(mui的三级联动需要的是json树形式的数据)

原文代码连接

阿里fastjson将集合转为树形结构

package com.etoak.test;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

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

/**
 * Created by wen on 2019/6/20.
 */
public class Test {
    public static void main(String[] args) {
        List<Map<String, Object>> data = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("id", 1);
        map.put("pid", 0);
        map.put("name", "甘肃省");
        data.add(map);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("id", 2);
        map2.put("pid", 1);
        map2.put("name", "天水市");
        data.add(map2);
        Map<String, Object> map3 = new HashMap<>();
        map3.put("id", 3);
        map3.put("pid", 2);
        map3.put("name", "秦州区");
        data.add(map3);
        Map<String, Object> map4 = new HashMap<>();
        map4.put("id", 4);
        map4.put("pid", 0);
        map4.put("name", "北京市");
        data.add(map4);
        Map<String, Object> map5 = new HashMap<>();
        map5.put("id", 5);
        map5.put("pid", 4);
        map5.put("name", "昌平区");
        data.add(map5);
        //System.out.println(JSON.toJSONString(data));
        JSONArray result = listToTree(JSONArray.parseArray(JSON.toJSONString(data)), "id", "pid", "children");
        //System.out.println(JSON.toJSONString(result));
    }
    public static JSONArray listToTree(JSONArray arr,String id,String pid,String child){
        JSONArray r = new JSONArray();
        JSONObject hash = new JSONObject();
        //将数组转为Object的形式,key为数组中的id
        for(int i=0;i<arr.size();i++){
            JSONObject json = (JSONObject) arr.get(i);
            hash.put(json.getString(id), json);
        }

        //遍历结果集
        for(int j=0;j<arr.size();j++){
            System.out.println("此轮Hash:"+hash);
            System.out.println("此轮arr:"+arr);
            //单条记录
            JSONObject aVal = (JSONObject) arr.get(j);
            System.out.println("初始的aVal:"+aVal);
            //在hash中取出key为单条记录中pid的值
            JSONObject hashVP = (JSONObject) hash.get(aVal.get(pid).toString());
            System.out.println("初始的hashVP:"+hashVP);

            //如果记录的pid存在,则说明它有父节点,将她添加到孩子节点的集合中
            if(hashVP!=null){
                System.out.println("hashVP不为空进行操作");
                //检查是否有child属性
                if(hashVP.get(child)!=null){
                    System.out.println("有孩子");
                    JSONArray ch = (JSONArray) hashVP.get(child);
                    ch.add(aVal);
                    hashVP.put(child, ch);
                }else{
                    System.out.println("没孩子");
                    JSONArray ch = new JSONArray();
                    ch.add(aVal);
                    hashVP.put(child, ch);
                }

            }else{
                System.out.println("hashVP为空直接r添加");

                r.add(aVal);

            }
            System.out.println("变化后的hashVP:"+hashVP);
            System.out.println("最后的对象是:"+r);

            System.out.println("=====");
        }
        return r;
    }
}

需要的数据格式如下:

[          {
				value: '0',
				text: '南京市',
				children: [{
					value: "1",
					text: "某某区",
					children: [{
						value: "2",
						text: "街道1"
					}, {
						value: "3",
						text: "街道2"
					}, {
						value: "4",
						text: "街道3"
					}],
				   },{
							value: "5",
							text: "某某区",
							children: [{
								value: "6",
								text: "街道1"
							}, {
								value: "7",
								text: "街道2"
							}, {
								value: "8",
								text: "街道3"
							}],
				      }],

			}]

用到了Alibaba的fastJson

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值