From 0d2203b4027ae3bf524d63da67202e778648e4cf Mon Sep 17 00:00:00 2001
From: jinlin <jinlin>
Date: 星期五, 02 八月 2024 09:59:20 +0800
Subject: [PATCH] 修改
---
zt/common/src/main/java/com/zt/common/utils/JsonUtils2.java | 49 +++++++++++++++++++++++++++++++++++++++----------
1 files changed, 39 insertions(+), 10 deletions(-)
diff --git a/zt/common/src/main/java/com/zt/common/utils/JsonUtils2.java b/zt/common/src/main/java/com/zt/common/utils/JsonUtils2.java
index 91abaf9..d80e6ce 100644
--- a/zt/common/src/main/java/com/zt/common/utils/JsonUtils2.java
+++ b/zt/common/src/main/java/com/zt/common/utils/JsonUtils2.java
@@ -1,26 +1,55 @@
package com.zt.common.utils;
+import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import org.apache.commons.lang3.ArrayUtils;
public class JsonUtils2 {
- public static boolean setArrJsonValueByPath(JSONObject pJSONObject, String[] path, String searchKey, String searchValue, String targetKey, Object value) {
+ /* public static boolean setArrJsonValueByPath(JSONObject pJSONObject, String[] path, String searchKey, String searchValue, String targetKey, Object value) {
+ if (path.length == 1) {
+ JSONArray jsonArray = pJSONObject.getJSONArray(path[0]);
+ for (int i = 0; i < jsonArray.size(); i++
+ ) {
+ JSONObject jsonObject = jsonArray.getJSONObject(i);
+ if (jsonObject.get(searchKey).equals(searchValue)) {
+ jsonObject.put(targetKey, value);
+ }
+ }
+ return true;
+ } else {
+ JSONObject sJSONObject = pJSONObject.getJSONObject(path[0]);
+ path = ArrayUtils.remove(path, 0);
+ setArrJsonValueByPath(sJSONObject, path, searchKey, searchValue, targetKey, value);
+ }
+ return false;
+ }*/
+ public static boolean setArrJsonValueByPath(JSONObject pJSONObject, String[] path, String searchKey, String searchValue, String path2, Object value) {
+ return setArrJsonValueByPath(pJSONObject, path, searchKey, searchValue, path2.split("/"), value);
+
+ }
+
+ public static boolean setArrJsonValueByPath(JSONObject pJSONObject, String[] path, String searchKey, String searchValue, String[] path2, Object value) {
if (path.length == 1) {
JSONArray jsonArray = pJSONObject.getJSONArray(path[0]);
- for (int i = 0; i < jsonArray.size(); i++
- ) {
- JSONObject jsonObject = jsonArray.getJSONObject(i);
- if (jsonObject.get(searchKey).equals(searchValue)) {
- jsonObject.put(targetKey, value);
+ JSONObject targetJSONObject = null;
+ if (searchKey != null) {
+ for (int i = 0; i < jsonArray.size(); i++) {
+ JSONObject jsonObject = jsonArray.getJSONObject(i);
+ if (jsonObject.get(searchKey).equals(searchValue)) {
+ targetJSONObject = jsonObject;
+ }
}
+ } else {
+ targetJSONObject = jsonArray.getJSONObject(0);
}
+ setJsonValueByPath(targetJSONObject, path2, value);
return true;
} else {
JSONObject sJSONObject = pJSONObject.getJSONObject(path[0]);
path = ArrayUtils.remove(path, 0);
- setArrJsonValueByPath(sJSONObject, path, searchKey, searchValue, targetKey, value);
+ setArrJsonValueByPath(sJSONObject, path, searchKey, searchValue, path2, value);
}
return false;
}
@@ -49,9 +78,9 @@
return true;
} else {
JSONObject sJSONObject = pJSONObject.getJSONObject(path[0]);
- if (sJSONObject==null){
+ if (sJSONObject == null) {
sJSONObject = new JSONObject("{}");
- pJSONObject.put(path[0],sJSONObject);
+ pJSONObject.put(path[0], sJSONObject);
}
path = ArrayUtils.remove(path, 0);
setJsonValueByPath(sJSONObject, path, value);
@@ -64,7 +93,7 @@
return pJSONObject.get(path[0]);
} else {
JSONObject sJSONObject = pJSONObject.getJSONObject(path[0]);
- if (sJSONObject==null)
+ if (sJSONObject == null)
return null;
path = ArrayUtils.remove(path, 0);
return getJsonValueByPath(sJSONObject, path);
--
Gitblit v1.9.1