From 583db0ddcbafe6d90e23172017583e4214084965 Mon Sep 17 00:00:00 2001 From: ximu-tao Date: Sun, 7 Sep 2025 12:09:27 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E6=8F=92=E4=BB=B6DIY=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/utils/json/JsonModuleLoader.java | 52 +++++++++++++++---- .../diy/{compoents.json => components.json} | 0 2 files changed, 41 insertions(+), 11 deletions(-) rename webroot/addon/recharge/java/src/main/resources/recharge/loader/diy/{compoents.json => components.json} (100%) diff --git a/niucloud-core/src/main/java/com/niu/core/common/utils/json/JsonModuleLoader.java b/niucloud-core/src/main/java/com/niu/core/common/utils/json/JsonModuleLoader.java index 4b411acf..ea5cf52a 100644 --- a/niucloud-core/src/main/java/com/niu/core/common/utils/json/JsonModuleLoader.java +++ b/niucloud-core/src/main/java/com/niu/core/common/utils/json/JsonModuleLoader.java @@ -317,23 +317,53 @@ public class JsonModuleLoader { private JSONObject mergeJsonForAttribute(Collection jsonObjectList) { JSONObject targetMap = new JSONObject(); for (JSONObject jsonObject : jsonObjectList) { - jsonObject.keySet().stream().forEach(key -> { - Object value = targetMap.get(key); - if (value == null) { - targetMap.put(key, jsonObject.get(key)); + for (String key : jsonObject.keySet()) { + Object value = jsonObject.get(key); + if (targetMap.containsKey(key)) { + // 如果targetMap中已经有该key,且value是JSONObject,则递归合并 + Object existingValue = targetMap.get(key); + if (existingValue instanceof JSONObject && value instanceof JSONObject) { + JSONObject merged = mergeJsonObjects((JSONObject) existingValue, (JSONObject) value); + targetMap.put(key, merged); + } else { + // 否则,用新的value覆盖(或者根据需求处理,这里直接覆盖) + targetMap.put(key, value); + } } else { - JSONObject jsonValue1 = (JSONObject) value; - JSONObject jsonValue2 = (JSONObject) jsonObject.get(key); - jsonValue2.keySet().stream().forEach(k -> { - jsonValue1.put(k, jsonValue2.get(k)); - }); - targetMap.put(key, jsonValue1); + targetMap.put(key, value); } - }); + } } return targetMap; } + // 递归合并两个JSONObject + private JSONObject mergeJsonObjects(JSONObject obj1, JSONObject obj2) { + JSONObject result = new JSONObject(); + // 先添加obj1的所有键值对 + for (String key : obj1.keySet()) { + result.put(key, obj1.get(key)); + } + // 合并obj2的键值对 + for (String key : obj2.keySet()) { + Object value2 = obj2.get(key); + if (result.containsKey(key)) { + Object value1 = result.get(key); + // 如果两个值都是JSONObject,则递归合并 + if (value1 instanceof JSONObject && value2 instanceof JSONObject) { + JSONObject merged = mergeJsonObjects((JSONObject) value1, (JSONObject) value2); + result.put(key, merged); + } else { + // 否则,用obj2的值覆盖 + result.put(key, value2); + } + } else { + result.put(key, value2); + } + } + return result; + } + /** * @param jsonFile * @return diff --git a/webroot/addon/recharge/java/src/main/resources/recharge/loader/diy/compoents.json b/webroot/addon/recharge/java/src/main/resources/recharge/loader/diy/components.json similarity index 100% rename from webroot/addon/recharge/java/src/main/resources/recharge/loader/diy/compoents.json rename to webroot/addon/recharge/java/src/main/resources/recharge/loader/diy/components.json -- Gitee