无情替换JSON 中的数据

无情替换JSON 中的数据

1新塞入的值可以是Map list json… 任意对象
2 被替换的值必须是单值 String int bool
3 keyValue 固定 是 String
补充:: 新塞入的值 如果是POJO,必须是包装类型,否则 int 默认为0,会默认塞入


/**
     * 根据key value 替换原位置数据
     *
     * @param oldJson  JSON or JSONArray
     * @param newValue JSON JSONArray or String int bool
     * @param keyValue String
     * @param oldValue String int bool
     * @return
     */

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

import java.util.Map;


public class WJsonUtil {


    /**
     * 获取指定json属性对应得数据
     *
     * @param objJson
     * @param reg
     * @return
     */
    public static Object getValueByKey(Object objJson, String reg) {
        //如果obj为json数组
        if (objJson instanceof JSONArray) {
            JSONArray objArray = (JSONArray) objJson;
            for (int i = 0; i < objArray.size(); i++) {
                return getValueByKey(objArray.get(i), reg);
            }
        }
        //如果为json对象
        else if (objJson instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) objJson;
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                String key = entry.getKey();
                if (reg.equals(key)) {
                    return entry.getValue();
                }
                Object o = find(reg, jsonObject, key);
                if (null != o) {
                    return o;
                }
            }
        }
        return null;
    }

    /**
     * 获取指定json属性对应得数据
     *
     * @param objJson
     * @param reg
     * @return
     */
    public static boolean isValueByKey(Object objJson, String reg) {
        //如果obj为json数组
        if (objJson instanceof JSONArray) {
            JSONArray objArray = (JSONArray) objJson;
            for (int i = 0; i < objArray.size(); i++) {
                getValueByKey(objArray.get(i), reg);
            }
        }
        //如果为json对象
        else if (objJson instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) objJson;
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                String key = entry.getKey();
                if (reg.equals(key)) {
                    return true;
                }
                find(reg, jsonObject, key);
            }
        }
        return false;
    }

    public static Object replaceValueByKeyAndValue(
            Object oldJson,
            Object newValue0, String keyValue0, Object oldValue0,
            Object newValue1, String keyValue1, Object oldValue1) {
        replaceValueByKeyAndValue(oldJson,newValue0, keyValue0, oldValue0);
        replaceValueByKeyAndValue(oldJson,newValue1, keyValue1, oldValue1);
        return oldJson;
    }
    public static Object replaceValueByKeyAndValue(
            Object oldJson,
            Object newValue0, String keyValue0, Object oldValue0,
            Object newValue1, String keyValue1, Object oldValue1,
            Object newValue2, String keyValue2, Object oldValue2) {
        replaceValueByKeyAndValue(oldJson,newValue0, keyValue0, oldValue0);
        replaceValueByKeyAndValue(oldJson,newValue1, keyValue1, oldValue1);
        replaceValueByKeyAndValue(oldJson,newValue2, keyValue2, oldValue2);
        return oldJson;
    }

    public static Object replaceValueByKeyAndValue(
            Object oldJson,
            Object newValue0, String keyValue0, Object oldValue0,
            Object newValue1, String keyValue1, Object oldValue1,
            Object newValue2, String keyValue2, Object oldValue2,
            Object newValue3, String keyValue3, Object oldValue3) {
        replaceValueByKeyAndValue(oldJson,newValue0, keyValue0, oldValue0);
        replaceValueByKeyAndValue(oldJson,newValue1, keyValue1, oldValue1);
        replaceValueByKeyAndValue(oldJson,newValue2, keyValue2, oldValue2);
        replaceValueByKeyAndValue(oldJson,newValue3, keyValue3, oldValue3);
        return oldJson;
    }

    public static Object replaceValueByKeyAndValue(
            Object oldJson,
            Object newValue0, String keyValue0, Object oldValue0,
            Object newValue1, String keyValue1, Object oldValue1,
            Object newValue2, String keyValue2, Object oldValue2,
            Object newValue3, String keyValue3, Object oldValue3,
            Object newValue4, String keyValue4, Object oldValue4) {
        replaceValueByKeyAndValue(oldJson,newValue0, keyValue0, oldValue0);
        replaceValueByKeyAndValue(oldJson,newValue1, keyValue1, oldValue1);
        replaceValueByKeyAndValue(oldJson,newValue2, keyValue2, oldValue2);
        replaceValueByKeyAndValue(oldJson,newValue3, keyValue3, oldValue3);
        replaceValueByKeyAndValue(oldJson,newValue4, keyValue4, oldValue4);
        return oldJson;
    }

    public static Object replaceValueByKeyAndValue(
            Object oldJson,
            Object newValue0, String keyValue0, Object oldValue0,
            Object newValue1, String keyValue1, Object oldValue1,
            Object newValue2, String keyValue2, Object oldValue2,
            Object newValue3, String keyValue3, Object oldValue3,
            Object newValue4, String keyValue4, Object oldValue4,
            Object newValue5, String keyValue5, Object oldValue5) {
        replaceValueByKeyAndValue(oldJson,newValue0, keyValue0, oldValue0);
        replaceValueByKeyAndValue(oldJson,newValue1, keyValue1, oldValue1);
        replaceValueByKeyAndValue(oldJson,newValue2, keyValue2, oldValue2);
        replaceValueByKeyAndValue(oldJson,newValue3, keyValue3, oldValue3);
        replaceValueByKeyAndValue(oldJson,newValue4, keyValue4, oldValue4);
        replaceValueByKeyAndValue(oldJson,newValue5, keyValue5, oldValue5);
        return oldJson;
    }
    public static Object replaceValueByKeyAndValue(
            Object oldJson,
            Object newValue0, String keyValue0, Object oldValue0,
            Object newValue1, String keyValue1, Object oldValue1,
            Object newValue2, String keyValue2, Object oldValue2,
            Object newValue3, String keyValue3, Object oldValue3,
            Object newValue4, String keyValue4, Object oldValue4,
            Object newValue5, String keyValue5, Object oldValue5,
            Object newValue6, String keyValue6, Object oldValue6) {
        replaceValueByKeyAndValue(oldJson,newValue0, keyValue0, oldValue0);
        replaceValueByKeyAndValue(oldJson,newValue1, keyValue1, oldValue1);
        replaceValueByKeyAndValue(oldJson,newValue2, keyValue2, oldValue2);
        replaceValueByKeyAndValue(oldJson,newValue3, keyValue3, oldValue3);
        replaceValueByKeyAndValue(oldJson,newValue5, keyValue5, oldValue5);
        replaceValueByKeyAndValue(oldJson,newValue6, keyValue6, oldValue6);
        return oldJson;
    }



    /**
     * 根据key value 替换原位置数据
     *
     * @param oldJson  JSON or JSONArray
     * @param newValue JSON JSONArray or String int bool
     * @param keyValue String
     * @param oldValue
     * @return
     */
    public static Object replaceValueByKeyAndValue(Object oldJson, Object newValue, String keyValue, Object oldValue) {
        //如果obj为json数组
        if (oldJson instanceof JSONArray) {
            isJSONArrayComplex((JSONArray) oldJson, newValue, keyValue, oldValue);
        }
        //如果为json对象
        else if (oldJson instanceof JSONObject) {
            isJSONObjectComplex((JSONObject) oldJson, newValue, keyValue, oldValue);
        }
        return oldJson;
    }

    /**
     * 根据key 替换原位置数据
     *
     * @param oldJson
     * @param newValue
     * @param keyValue
     * @return
     */
    public static Object replaceValueByKey(Object oldJson, Object newValue, String keyValue) {
        //如果obj为json数组
        if (oldJson instanceof JSONArray) {
            isJSONArray((JSONArray) oldJson, newValue, keyValue);
        }
        //如果为json对象
        else if (oldJson instanceof JSONObject) {
            isJSONObject((JSONObject) oldJson, newValue, keyValue);
        }
        return oldJson;
    }

    private static void isJSONArrayComplex(JSONArray oldJson, Object newValue, String keyValue, Object oldValue) {
        JSONArray objArray = oldJson;
        for (int i = 0; i < objArray.size(); i++) {
            Object valueByKey = objArray.get(i);
            if (valueByKey instanceof JSONObject) {
                isJSONObjectComplex((JSONObject) valueByKey, newValue, keyValue, oldValue);
            }
            if (valueByKey instanceof JSONArray) {
                isJSONArrayComplex((JSONArray) valueByKey, newValue, keyValue, oldValue);
            }

        }
    }

    private static void isJSONArray(JSONArray oldJson, Object newValue, String keyValue) {
        JSONArray objArray = oldJson;
        for (int i = 0; i < objArray.size(); i++) {
            Object valueByKey = objArray.get(i);
            if (valueByKey instanceof JSONObject) {
                isJSONObject((JSONObject) valueByKey, newValue, keyValue);
            }
            if (valueByKey instanceof JSONArray) {
                isJSONArray((JSONArray) valueByKey, newValue, keyValue);
            }

        }
    }

    private static void isJSONObjectComplex(JSONObject oldJson, Object newValue, String keyValue, Object oldValue) {
        JSONObject jsonObject = oldJson;
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (value != null && !(value instanceof JSONObject) && !(value instanceof JSONArray)) {
                if (keyValue.equals(key) && String.valueOf(value).equals(String.valueOf(oldValue))) {
                    entry.setValue(newValue);
                }
            }

            Object object = jsonObject.get(key);
            //如果得到的是数组
            if (object instanceof JSONArray) {
                JSONArray objArray = (JSONArray) object;
                replaceValueByKeyAndValue(objArray, newValue, keyValue, oldValue);
            }
            //如果key中是一个json对象
            else if (object instanceof JSONObject) {
                replaceValueByKeyAndValue(object, newValue, keyValue, oldValue);
            }
        }
    }

    private static void isJSONObject(JSONObject oldJson, Object newValue, String keyValue) {
        JSONObject jsonObject = oldJson;
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            String key = entry.getKey();
            if (keyValue.equals(key)) {
                entry.setValue(newValue);
            }
            Object object = jsonObject.get(key);
            //如果得到的是数组
            if (object instanceof JSONArray) {
                JSONArray objArray = (JSONArray) object;
                replaceValueByKey(objArray, newValue, keyValue);
            }
            //如果key中是一个json对象
            else if (object instanceof JSONObject) {
                replaceValueByKey(object, newValue, keyValue);
            }
        }
    }

    private static Object find(String reg, JSONObject jsonObject, String key) {
        Object object = jsonObject.get(key);
        //如果得到的是数组
        if (object instanceof JSONArray) {
            JSONArray objArray = (JSONArray) object;
            return getValueByKey(objArray, reg);
        }
        //如果key中是一个json对象
        else if (object instanceof JSONObject) {
            return getValueByKey(object, reg);
        }
        return null;
    }
    
}



举例

StrUtil 是hutool 的工具类

public static void main(String[] args) {
        String a = "{\n" +
                "  \"header\": {\n" +
                "    \"uid\": \"302285928\",\n" +
                "    \"user_id\": \"302285928\",\n" +
                "  },\n" +
                "  \"body\": {\n" +
                "    \"human1\": \"cfa79e62-0336-47aa-b64b-e5982890f109\",\n" +
                "    \"human2\": \"499e828e-5096-463f-90b6-43169548c8fc\",\n" +
                "    \"human3\": \"kc62426a8776a06\",\n" +
                "    \"amount\": 9,\n" +
                "    \"extend_params\": \"\"\n" +
                "  }\n" +
                "}";

        JSONObject aa = JSON.parseObject(a, JSONObject.class);
        System.out.println(StrUtil.format("转化前的 json:{}",aa.toJSONString()));
        replaceValueByKeyAndValue(
                aa,
                Map.of("诗人","普希金"),"human1","cfa79e62-0336-47aa-b64b-e5982890f109",
                "康德","human2","499e828e-5096-463f-90b6-43169548c8fc",
                List.of(1,2,3),"human3","kc62426a8776a06"
        );
        System.out.println("//-------------------- elegant splite line --------------------//");
        System.out.println(StrUtil.format("转化后的 json:{}",aa.toJSONString()));
    }

效果:

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值