• Java Fastjson 替换指定属性 & JQ风格


    Fastjson   是一个 Java 库,可以将 Java 对象转换为 JSON 格式,当然它也可以将 JSON 字符串转换为 Java 对象。

    功能: 在不知道JSON格式的情况下,根据路径替换属性。

    首先添加依赖,因为我们需要这个库。

         <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.47</version>
            </dependency>

    添加一个测试工具类 JsonUtil

    package cn.flkj.sshluciaextension.utils;
    
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    
    /**
     * @author : 州长在手 2021/3/1 下午4:11
     */
    public class JsonUtil {
    
        // --------------------------------- NO ARRAY ---------------------------------
    //    public static <T> JSONObject JsonReplace(JSONObject json, String[] jsonPath, T content) {
    //        return JsonReplaceHelper(json, jsonPath, content, 1);
    //    }
    //
    //    public static <T> JSONObject JsonReplaceHelper(JSONObject json, String[] jsonPath, T content, int index) {
    //        if (index == jsonPath.length) {
    //            return json.fluentPut(jsonPath[index - 1], content);
    //        }
    //        JSONObject jsonSub = json.getJSONObject(jsonPath[index - 1]);
    //        JSONObject jsonSubX = JsonReplaceHelper(jsonSub, jsonPath, content, index + 1);
    //        json.fluentPut(jsonPath[index - 1], jsonSubX);
    //        return json;
    //    }
    
        // --------------------------------- ARRAY ---------------------------------
        public static <T> void JsonReplace(JSONObject json, String[] jsonPath, T content) {
             JsonReplaceHelper(json, jsonPath, content, 1);
        }
    
        public static <T> JSONObject JsonReplaceHelper(JSONObject json, String[] jsonPath, T content, int index) {
            String subPath = jsonPath[index - 1];
            // 递归结束状态,返回给上一次递归需要的数据
            if (index == jsonPath.length) {
                if (!subPath.contains("[")) {
                    return json.fluentPut(jsonPath[index - 1], content);
                } else {
                    int number = Integer.parseInt(subPath.substring(subPath.indexOf("[") + 1, subPath.indexOf("]")));
                    String subPathX = subPath.substring(0, subPath.indexOf("["));
                    JSONArray subArray = json.getJSONArray(subPathX);
                    subArray.remove(number);
                    subArray.fluentAdd(number, content);
                    json.fluentPut(subPathX, subArray);
                    return json;
                }
            }
            // 是否是数组
            if (!subPath.contains("[")) {
                JSONObject jsonSub = json.getJSONObject(subPath);
                JSONObject jsonSubX = JsonReplaceHelper(jsonSub, jsonPath, content, index + 1);
                json.fluentPut(subPath, jsonSubX);
                return json;
            } else {
                // 数组的第几个
                int number = Integer.parseInt(subPath.substring(subPath.indexOf("[") + 1, subPath.indexOf("]")));
                // 包含数组的真实键
                String subPathX = subPath.substring(0, subPath.indexOf("["));
                JSONArray subArray = json.getJSONArray(subPathX);
                JSONObject jsonSub = subArray.getJSONObject(number);
                JSONObject jsonSubX = JsonReplaceHelper(jsonSub, jsonPath, content, index + 1);
                subArray.remove(number);
                subArray.fluentAdd(number, jsonSubX);
                json.fluentPut(subPathX, subArray);
                return json;
            }
        }
    }
    package cn.flkj.sshluciaextension.utils;
    
    import cn.flkj.sshluciaextension.shellcommand.ShellCommand;
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import org.bouncycastle.math.ec.ScaleYNegateXPointMap;
    import org.junit.jupiter.api.Test;
    
    import static org.junit.jupiter.api.Assertions.*;
    
    /**
     * @author : 州长在手 2021/3/1 下午4:15
     */
    class JsonUtilTest {
        //
        @Test
        public void testJsonReplace(){
            String jsonStr = ShellCommand.readingFile("static/config.txt");
            // System.out.println(jsonStr);
            JSONObject json = JSON.parseObject(jsonStr);
            System.out.println("old: "+json);
         // 需要替换属性的路径; String rep
    = "data.data[0].payload.data[1].config[0]";
    // 替换的属性 String con
    = "替换成功"; JsonUtil.JsonReplace(json,rep.split("\."),con); System.out.println("new: "+json); } }

    测试用例:

    {
        "data":{
            "data":[
                {
                    "payload":{
                        "data":[
                            {
                                "axss":"aaaaaa",
                                "config":[
                                    "-----------------------"
                                ]
                            },
                            {
                                "axss":"aaaaaa",
                                "config":[
                                    "需要替换"
                                ]
                            }
                        ]
                    }
                },
                {
                    "payload":{
                        "data":{
                            "axss":"aaaaaa",
                            "config":"xxxxx"
                        }
                    }
                }
            ]
        }
    }
    

    测试截图:

  • 相关阅读:
    Java基本概念
    Java基础语法
    Java环境的搭建
    elicpse
    常见编译器EOP
    上传突破学习笔记
    认识ollydbg
    160个Crackerme破解
    python基础(1)
    文件上传
  • 原文地址:https://www.cnblogs.com/Addoil/p/14467767.html
Copyright © 2020-2023  润新知