FastJSON自定义工具类

废话不多说,直接上代码

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

import java.util.*;

/**
 * @Author: J
 * @Version: 1.0
 * @CreateTime: 2023/5/9
 * @Description: JSON工具类
 **/
public class JSONUtils {

    /**
     *@Autor J
     *@Param jsonStr
     *@return JSONObject
     *@CreateTime 2023/5/10 11:05
     *@Version 1.0
     *@Description TODO 将JSON 字符串转成 JSONObject
    **/
    public static JSONObject str2JSONObject(String jsonStr) {
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        return jsonObject;
    }

    /**
     * @return int
     * @Autor J
     * @Param jsonStr
     * @CreateTime 2023/5/9 14:38
     * @Version 1.0
     * @Description TODO 获取JSON字符串层级
     **/
    public static int getJSONDepth(String jsonStr) {
        JSONObject jsonObject = str2JSONObject(jsonStr);
        return getJSONDepth(jsonObject, 1);
    }

    /**
     * @return int
     * @Autor J
     * @Param jsonObject
     * @Param depth
     * @CreateTime 2023/5/9 14:38
     * @Version 1.0
     * @Description TODO 获取JSON Object对象层级
     **/
    public static int getJSONDepth(JSONObject jsonObject, int depth) {
        int maxDepth = depth;
        for (String key : jsonObject.keySet()) {
            Object value = jsonObject.get(key);
            if (value instanceof JSONObject) {
                int childDepth = getJSONDepth((JSONObject) value, depth + 1);
                if (childDepth > maxDepth) {
                    maxDepth = childDepth;
                }
            }
        }
        return maxDepth;
    }

    /**
     * @return Set<String>
     * @Autor J
     * @Param jsonObject
     * @Param level
     * @Param currentLevel
     * @CreateTime 2023/5/9 14:50
     * @Version 1.0
     * @Description TODO 根据指定层级返回key
     **/
    public static Set<String> getJSONKeyByLevel(Object json, int level, int currentLevel) {
        Set<String> keys = new HashSet<String>();
        if (currentLevel == level) {
            if (json instanceof JSONObject) {
                JSONObject jsonObject = (JSONObject) json;
                for (String key : jsonObject.keySet()) {
                    keys.add(key);
                }
            }
        } else {
            if (json instanceof JSONObject) {
                JSONObject jsonObject = (JSONObject) json;
                for (String key : jsonObject.keySet()) {
                    keys.addAll(getJSONKeyByLevel(jsonObject.get(key), level, currentLevel + 1));
                }
            } else if (json instanceof JSONArray) {
                JSONArray jsonArray = (JSONArray) json;
                for (Object obj : jsonArray) {
                    keys.addAll(getJSONKeyByLevel(obj, level, currentLevel + 1));
                }
            }
        }
        return keys;
    }

    /**
     * @return Object
     * @Autor J
     * @Param jsonObject
     * @Param key
     * @CreateTime 2023/5/9 15:46
     * @Version 1.0
     * @Description TODO 根据指定key返回value
     **/
    public static Object getValueByKey(JSONObject jsonObject, String key) {
        if (jsonObject.containsKey(key)) {
            return jsonObject.get(key);
        } else {
            for (String k : jsonObject.keySet()) {
                Object value = jsonObject.get(k);
                if (value instanceof JSONObject) {
                    Object result = getValueByKey((JSONObject) value, key);
                    if (result != null) {
                        return result;
                    }
                } else if (value instanceof JSONArray) {
                    JSONArray array = (JSONArray) value;
                    for (int i = 0; i < array.size(); i++) {
                        Object item = array.get(i);
                        if (item instanceof JSONObject) {
                            Object result = getValueByKey((JSONObject) item, key);
                            if (result != null) {
                                return result;
                            }
                        }
                    }
                }
            }
            return null;
        }
    }

    /**
     *@Autor J
     *@Param jsonObject
     *@return ArrayList<JSONObject>
     *@CreateTime 2023/5/10 10:44
     *@Version 1.0
     *@Description TODO 将JSONObject中1级key都以单独的JSONObject存储到ArrayList中
    **/
    public static List<JSONObject> getFirstJSONList(JSONObject jsonObject) {
        ArrayList<JSONObject> jsonObjectArrayList = new ArrayList<JSONObject>();
        // 获取JSONObject中所有1级key
        Set<String> firstJSONKeys = getJSONKeyByLevel(jsonObject, 1, 1);
        for (String firstKey : firstJSONKeys) {
            JSONObject levelJSONObject = new JSONObject();
            // 将1级key及value以JSONObject存储到List中
            levelJSONObject.put(firstKey, jsonObject.getJSONObject(firstKey));
            jsonObjectArrayList.add(levelJSONObject);
        }
        return jsonObjectArrayList;
    }

    /**
     *@Autor J
     *@Param jsonObject
     *@return List<JSONObject>
     *@CreateTime 2023/5/12 17:33
     *@Version 1.0
     *@Description TODO 获取第二级JSON,以一级key + 二级key分组组成新的JSON并存如List中
    **/
    public static List<JSONObject> getSecondJSONList(JSONObject jsonObject) {
        ArrayList<JSONObject> jsonObjectList = new ArrayList<JSONObject>();
        // 获取一级key
        String firstKey = jsonObject.keySet().iterator().next();
        // 获取二级JSON
        JSONObject secondJSON = (JSONObject) jsonObject.get(firstKey);
        // 将二级JSON打散成一个个JSON对象存储List中
        List<JSONObject> secondJSONList = getFirstJSONList(secondJSON);
        // 遍历打散的二级JSON List,将一级key作为key,将打散的二级JSONObject最为value进行存储
        for (JSONObject object : secondJSONList) {
            JSONObject newJSON = new JSONObject();
            newJSON.put(firstKey, object);
            jsonObjectList.add(newJSON);
        }
        return jsonObjectList;
    }

    /**
     *@Autor J
     *@Param jsonObject
     *@return List<JSONObject>
     *@CreateTime 2023/5/15 10:01
     *@Version 1.0
     *@Description TODO 获取第三级JSON,以一级key + 二级key + 三级key 分组组成新的JSON并存如List中
    **/
    public static List<JSONObject> getThirdJSONList(JSONObject jsonObject) {
        ArrayList<JSONObject> jsonObjects = new ArrayList<JSONObject>();
        // 获取一级key
        String firstKey = jsonObject.keySet().iterator().next();
        // 获取二级Key
        String secondKey = getJSONKeyByLevel(jsonObject, 2, 1).iterator().next();
        // 获取三级JSON
        JSONObject thirdJSON = (JSONObject) getValueByKey(jsonObject, secondKey);
        // 将三级JSON打散成一个个JSON对象存储到List中
        List<JSONObject> thirdJSONList = getFirstJSONList(thirdJSON);
        // 遍历打散的三级JSON List将一级key和二级key整合到打散的三级JSON中
        for (JSONObject object : thirdJSONList) {
            // 用以存储二级key,和三级JSON
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put(secondKey, object);
            // 用以存储一级key和上文拼接好的二级JSON
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put(firstKey, jsonObject1);
            // 将结果添加到List中
            jsonObjects.add(jsonObject2);
        }
        return jsonObjects;
    }

    /**
     *@Autor J
     *@Param jsonObject
     *@return List<JSONObject>
     *@CreateTime 2023/5/15 10:06
     *@Version 1.0
     *@Description TODO 以JSON前三级key为分组变成一个个JSON对象存储到List中
    **/
    public static List<JSONObject> getGroupBy3LevelJSONList(JSONObject jsonObject) {
        ArrayList<JSONObject> groupBy3LevelJSONList = new ArrayList<JSONObject>();
        // 获取一级JSON List
        List<JSONObject> firstJSONList = getFirstJSONList(jsonObject);
        for (JSONObject object : firstJSONList) {
            // 获取二级JSON List
            List<JSONObject> secondJSONList = getSecondJSONList(object);
            for (JSONObject jsonObject1 : secondJSONList) {
                // 获取三级JSON List
                List<JSONObject> thirdJSONList = getThirdJSONList(jsonObject1);
                // 将结果添加到List中
                groupBy3LevelJSONList.addAll(thirdJSONList);
            }
        }
        return groupBy3LevelJSONList;
    }
    /**
     *@Autor J
     *@Param jsonObject
     *@Param key
     *@return String
     *@CreateTime 2023/5/10 16:48
     *@Version 1.0
     *@Description TODO 获取JSON当前key及其所有父级key并拼接成字符串
    **/
    public static String getParentKeys(JSONObject jsonObject, String key) {
        for (String k: jsonObject.keySet()) {
            Object v = jsonObject.get(k);
            if (k.equals(key)) {
                return k;
            } else if (v instanceof JSONObject) {
                String result = getParentKeys((JSONObject) v, key);
                if (result != null) {
                    return k + "," + result;
                }
            }
        }
        return null;
    }

    /**
     *@Autor J
     *@Param jsonObject
     *@Param targetKey
     *@return List<String>
     *@CreateTime 2023/5/11 11:44
     *@Version 1.0
     *@Description TODO 根据传入的key获取该key的子节点key
    **/
    public static List<String> getChildKeys(JSONObject jsonObject, String targetKey) {
        ArrayList<String> childKeys = new ArrayList<String>();
        if (jsonObject == null || jsonObject.isEmpty()) {
            return childKeys;
        }
        for (String key : jsonObject.keySet()) {
            Object value = jsonObject.get(key);
            if (key.equals(targetKey)) {
                if (value instanceof JSONObject) {
                    for (String childKey : ((JSONObject) value).keySet()) {
                        childKeys.add(childKey);
                    }
                }
            } else if (value instanceof JSONObject) {
                childKeys.addAll(getChildKeys((JSONObject) value, targetKey));
            }
        }
        return childKeys;
    }

    /**
     *@Autor J
     *@Param jsonObject
     *@return List<String>
     *@CreateTime 2023/5/15 19:30
     *@Version 1.0
     *@Description TODO 将每个子节点递归拼接其父节点
    **/
    public static List<String> flattenJson(JSONObject jsonObject) {
        List<String> result = new ArrayList<String>();
        flattenJsonHelper(jsonObject, "", result);
        return result;
    }

    /**
     *@Autor J
     *@Param jsonObject
     *@Param prefix
     *@Param result
     *@return void
     *@CreateTime 2023/5/15 19:31
     *@Version 1.0
     *@Description TODO 递归拼接父节点
    **/
    private static void flattenJsonHelper(JSONObject jsonObject, String prefix, List<String> result) {
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            String newKey = prefix.isEmpty() ? key : prefix + "," + key;
            result.add(newKey);
            if (value instanceof JSONObject) {
                flattenJsonHelper((JSONObject) value, newKey, result);
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值