无情替换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 {
public static Object getValueByKey(Object objJson, String reg) {
if (objJson instanceof JSONArray) {
JSONArray objArray = (JSONArray) objJson;
for (int i = 0; i < objArray.size(); i++) {
return getValueByKey(objArray.get(i), reg);
}
}
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;
}
public static boolean isValueByKey(Object objJson, String reg) {
if (objJson instanceof JSONArray) {
JSONArray objArray = (JSONArray) objJson;
for (int i = 0; i < objArray.size(); i++) {
getValueByKey(objArray.get(i), reg);
}
}
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;
}
public static Object replaceValueByKeyAndValue(Object oldJson, Object newValue, String keyValue, Object oldValue) {
if (oldJson instanceof JSONArray) {
isJSONArrayComplex((JSONArray) oldJson, newValue, keyValue, oldValue);
}
else if (oldJson instanceof JSONObject) {
isJSONObjectComplex((JSONObject) oldJson, newValue, keyValue, oldValue);
}
return oldJson;
}
public static Object replaceValueByKey(Object oldJson, Object newValue, String keyValue) {
if (oldJson instanceof JSONArray) {
isJSONArray((JSONArray) oldJson, newValue, keyValue);
}
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);
}
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);
}
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);
}
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()));
}
效果:
|