java+JsonArray+多层嵌套去重

暂记于此,有空重新整理

package com.gk.health.interview.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther:
 * @Date: 2019/12/26/17:03
 * @Description:
 */
public class Test {

    public static void main(String[] args) {

        Long start = System.currentTimeMillis();

        String str = "[{'id':'1','name':'张家','parId':'','children':[{'id':'1','name':'张三','parId':'1'}]}]";
        String str1 = "[{'id':'1','name':'张家','parId':'','children':[{'id':'1','name':'张三','parId':'1'},{'id':'2','name':'张四','parId':'1'}]}]";
        String str2 = "[{'id':'2','name':'李家','parId':'','children':[{'id':'3','name':'李三','parId':'2'},{'id':'4','name':'李四','parId':'2'}]}]";
        String str3 = "[{'id':'2','name':'李家','parId':'','children':[{'id':'3','name':'李三','parId':'2'},{'id':'5','name':'李大','parId':'2'}]}]";
        String str4 = "[{'id':'3','name':'王家','parId':'','children':[{'id':'8','name':'王小','parId':'3'}]}]";
        Long end = System.currentTimeMillis();
        System.out.println(end - start+"ms---1");

        start = System.currentTimeMillis();
        JSONArray array = JSONArray.parseArray(str);
        JSONArray array1 = JSONArray.parseArray(str1);
        JSONArray array2 = JSONArray.parseArray(str2);
        JSONArray array3 = JSONArray.parseArray(str3);
        JSONArray array4 = JSONArray.parseArray(str4);

        end = System.currentTimeMillis();
        System.out.println(end - start+"ms---2");

        start = System.currentTimeMillis();
        array.addAll(array1);
        array.addAll(array2);
        array.addAll(array3);
        array.addAll(array4);
        end = System.currentTimeMillis();
        System.out.println(end - start+"ms---3");

        start = System.currentTimeMillis();
        dd(array);
        end = System.currentTimeMillis();
        System.out.println(end - start+"ms---4");

    }

    public static void dd(JSONArray arrays) {

        //创建新jsonAr
        JSONArray array = new JSONArray();
        //创建新的jsonarray,用于存储第二层菜单
        JSONArray jsonArray = new JSONArray();
        //遍历整个jsonarray
        for (int i=0;i<arrays.size();i++) {
            JSONObject object = arrays.getJSONObject(i);
            JSONArray array1 = JSON.parseArray("["+object.toString()+"]");
            array.addAll(array1);

            //第二层  children
            //获取children的jsonarray
            JSONArray array2 = object.getJSONArray("children");
            jsonArray.addAll(array2);
        }
        System.out.println("jsonArray: "+jsonArray);
        System.out.println("array: "+array);

        array = delRepeatIndexid(array);
        jsonArray = delRepeatIndexid(jsonArray);
        System.out.println("----------------");

        aa(array,jsonArray);
    }


    public static void bb(JSONArray arrays) {

        //创建新jsonAr
        JSONArray array = new JSONArray();
        //创建新的jsonarray,用于存储第二层菜单
        JSONArray jsonArray = new JSONArray();
        //遍历整个jsonarray
        for (int i=0;i<arrays.size();i++) {
            //新建map集合,用于转换json数组
            Map<String,Object> map = new HashMap<>();
            JSONObject object = arrays.getJSONObject(i);
            //取第一层数据的id,name存入新jsonArray 中
            String id = object.getString("id");
            String name = object.getString("name");

            //第二层  children
            //获取children的jsonarray
            JSONArray array1 = object.getJSONArray("children");
            //遍历array1
            for (int j = 0;j<array1.size();j++) {
                //创建map集合
                Map<String,Object> map1 = new HashMap<>();
                //获取array1
                JSONObject object1 = array1.getJSONObject(j);
                //获取id,name,parId
                String id1 = object1.getString("id");
                String name1 = object1.getString("name");
                String parId = object1.getString("parId");
                //判断当前id是否已经存在于array1中
                for (int k = 0;k<jsonArray.size();k++) {
                    JSONObject object2 = jsonArray.getJSONObject(k);
                    String id2 = object2.getString("id");
                    if (!id2.equals(id1)) {
                        map1.put("id",id1);
                        map1.put("name",name1);
                        map1.put("parId",parId);
                        jsonArray.add(map1);
                        break;
                    }
                }
                //第一次遍历直接新增
                if (jsonArray.size() < 1) {
                    map1.put("id",id1);
                    map1.put("name",name1);
                    map1.put("parId",parId);
                    jsonArray.add(map1);
                }
            }

            //判断id是否已经存在于array中
            for (int j = 0;j<array.size();j++) {
                JSONObject object1 = array.getJSONObject(j);
                String id1 = object1.getString("id");
                //不存在,则存入map中
                if (!id.equals(id1)) {
                    map.put("id",id);
                    map.put("name",name);
//                    map.put("children",jsonArray);
                    array.add(map);
                    break;
                }
            }
            //第一次遍历时,直接新增
            if (array.size() < 1) {
                map.put("id",id);
                map.put("name",name);
//                map.put("children",jsonArray);
                array.add(map);
            }
        }

        System.out.println("jsonArray: "+jsonArray);
        System.out.println("array: "+array);

        array = delRepeatIndexid(array);
        jsonArray = delRepeatIndexid(jsonArray);
        System.out.println("----------------");

        aa(array,jsonArray);


    }


    //合并为一个json
    public static void aa(JSONArray array,JSONArray jsonArray) {
        JSONArray array1 = new JSONArray();
        JSONArray array2 = new JSONArray();
        for (int i=0;i<array.size();i++) {
            List<Map<String,Object>> mapList = new ArrayList<>();
            JSONObject object = array.getJSONObject(i);
            String id = object.getString("id");
            for (int j=0;j<jsonArray.size();j++) {
                JSONObject jsonObject = jsonArray.getJSONObject(j);
                String parId = jsonObject.getString("parId");

                if (id.equals(parId)) {
                    Map<String,Object> map = new HashMap<>();
                    map.put("id",jsonObject.getString("id"));
                    map.put("name",jsonObject.getString("name"));
                    map.put("parId",jsonObject.getString("parId"));
                    mapList.add(map);
                }
            }
            object.put("children",mapList);
            System.out.println("object: "+object.toJSONString());
            array1 = JSONArray.parseArray("["+object.toString()+"]");
            array2.addAll(array1);
        }
        System.out.println("array2: "+array2);
    }

    /**
     * 根据Key去重复
     * @param array
     */
    public static JSONArray delRepeatIndexid(JSONArray array) {
        JSONArray arrayTemp = new JSONArray();
        int num = 0;
        for(int i = 0;i < array.size();i++){
            if(num==0){
                arrayTemp.add(array.get(i));
            }else{
                int numJ = 0;
                for(int j = 0;j < arrayTemp.size(); j++){
                    JSONObject newJsonObjectI = array.getJSONObject(i);
                    JSONObject newJsonObjectJ = arrayTemp.getJSONObject(j);
                    String index_idI = newJsonObjectI.getString("id");
                    String valueI = newJsonObjectI.getString("name");
                    String parIdI = newJsonObjectI.getString("parId");
                    String index_idJ = newJsonObjectJ.getString("id");
                    if(index_idI.equals(index_idJ)){
                        arrayTemp.remove(j);
                        JSONObject newObject = new JSONObject();
                        newObject.put("id", index_idI);
                        newObject.put("name", valueI);
                        newObject.put("parId", parIdI);
                        arrayTemp.add(newObject);
                        break;
                    }
                    numJ++;
                }
                if(numJ-1 == arrayTemp.size()-1){
                    arrayTemp.add(array.get(i));
                }
            }
            num++;
        }
        return arrayTemp;
    }

}

你可能感兴趣的:(Java)