From 69a3c374ca3afb770e3b9ffcbdda07ce362cbf58 Mon Sep 17 00:00:00 2001
From: 1 <1@123>
Date: 星期五, 09 一月 2026 19:59:29 +0800
Subject: [PATCH] #
---
rsf-open-api/src/main/java/com/vincent/rsf/openApi/utils/JsonReplaceTest.java | 192 ++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 192 insertions(+), 0 deletions(-)
diff --git a/rsf-open-api/src/main/java/com/vincent/rsf/openApi/utils/JsonReplaceTest.java b/rsf-open-api/src/main/java/com/vincent/rsf/openApi/utils/JsonReplaceTest.java
new file mode 100644
index 0000000..31db512
--- /dev/null
+++ b/rsf-open-api/src/main/java/com/vincent/rsf/openApi/utils/JsonReplaceTest.java
@@ -0,0 +1,192 @@
+package com.vincent.rsf.openApi.utils;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * JSON灞炴�у悕閫掑綊鏇挎崲娴嬭瘯绫�
+ *
+ * 婕旂ず濡備綍浣跨敤 FuncMap 杩涜JSON灞炴�у悕鐨勯�掑綊鏇挎崲
+ *
+ * @author vincent
+ * @since 2026-01-04
+ */
+public class JsonReplaceTest {
+
+ public static void main(String[] args) {
+ System.out.println("========== JSON灞炴�у悕閫掑綊鏇挎崲娴嬭瘯 ==========\n");
+
+ // 娴嬭瘯1锛氱畝鍗曞璞�
+ testSimpleObject();
+
+ // 娴嬭瘯2锛氬祵濂楀璞�
+ testNestedObject();
+
+ // 娴嬭瘯3锛氬寘鍚暟缁�
+ testWithArray();
+
+ // 娴嬭瘯4锛氬鏉傜粨鏋�
+ testComplexStructure();
+ }
+
+ /**
+ * 娴嬭瘯1锛氱畝鍗曞璞″睘鎬ф浛鎹�
+ */
+ private static void testSimpleObject() {
+ System.out.println("銆愭祴璇�1锛氱畝鍗曞璞°��");
+
+ // 鏋勯�犳祴璇曟暟鎹�
+ JSONObject data = new JSONObject();
+ data.put("orderNumber", "PO001");
+ data.put("orderQty", 100);
+ data.put("orderAmount", 5000.00);
+
+ System.out.println("鍘熷鏁版嵁锛�" + data.toJSONString());
+
+ // 瀹氫箟鏄犲皠瑙勫垯
+ Map<String, String> rules = new HashMap<>();
+ rules.put("orderNumber", "code");
+ rules.put("orderQty", "qty");
+ rules.put("orderAmount", "anfme");
+
+ // 鎵ц鏇挎崲
+ JSONObject result = ParamsMapUtils.replaceJsonKeys(data, rules);
+
+ System.out.println("鏇挎崲缁撴灉锛�" + result.toJSONString());
+ System.out.println();
+ }
+
+ /**
+ * 娴嬭瘯2锛氬祵濂楀璞℃繁搴︽浛鎹�
+ */
+ private static void testNestedObject() {
+ System.out.println("銆愭祴璇�2锛氬祵濂楀璞°��");
+
+ // 鏋勯�犲祵濂楁暟鎹�
+ JSONObject data = new JSONObject();
+ data.put("orderNumber", "PO002");
+
+ JSONObject customer = new JSONObject();
+ customer.put("customerName", "寮犱笁");
+ customer.put("customerPhone", "13800138000");
+
+ JSONObject address = new JSONObject();
+ address.put("cityName", "鍖椾含");
+ address.put("streetName", "鏈濋槼璺�88鍙�");
+ customer.put("address", address);
+
+ data.put("customer", customer);
+
+ System.out.println("鍘熷鏁版嵁锛�" + data.toJSONString());
+
+ // 瀹氫箟鏄犲皠瑙勫垯锛堜細搴旂敤鍒版墍鏈夊眰绾э級
+ Map<String, String> rules = new HashMap<>();
+ rules.put("orderNumber", "code");
+ rules.put("customerName", "name");
+ rules.put("customerPhone", "phone");
+ rules.put("cityName", "city");
+ rules.put("streetName", "street");
+
+ // 鎵ц閫掑綊鏇挎崲
+ JSONObject result = ParamsMapUtils.replaceJsonKeys(data, rules);
+
+ System.out.println("鏇挎崲缁撴灉锛�" + result.toJSONString());
+ System.out.println();
+ }
+
+ /**
+ * 娴嬭瘯3锛氬寘鍚暟缁勭殑鏇挎崲
+ */
+ private static void testWithArray() {
+ System.out.println("銆愭祴璇�3锛氬寘鍚暟缁勩��");
+
+ // 鏋勯�犲寘鍚暟缁勭殑鏁版嵁
+ JSONObject data = new JSONObject();
+ data.put("orderNumber", "PO003");
+
+ JSONArray items = new JSONArray();
+ for (int i = 1; i <= 3; i++) {
+ JSONObject item = new JSONObject();
+ item.put("materialCode", "MAT00" + i);
+ item.put("materialName", "鐗╂枡" + i);
+ item.put("itemQty", 10 * i);
+ items.add(item);
+ }
+ data.put("items", items);
+
+ System.out.println("鍘熷鏁版嵁锛�" + data.toJSONString());
+
+ // 瀹氫箟鏄犲皠瑙勫垯
+ Map<String, String> rules = new HashMap<>();
+ rules.put("orderNumber", "code");
+ rules.put("materialCode", "matnr");
+ rules.put("materialName", "maktx");
+ rules.put("itemQty", "qty");
+
+ // 鎵ц鏇挎崲锛堝寘鎷暟缁勪腑鐨勬墍鏈夊璞★級
+ JSONObject result = ParamsMapUtils.replaceJsonKeys(data, rules);
+
+ System.out.println("鏇挎崲缁撴灉锛�" + result.toJSONString());
+ System.out.println();
+ }
+
+ /**
+ * 娴嬭瘯4锛氬鏉傜粨鏋勶紙宓屽+鏁扮粍+澶氬眰锛�
+ */
+ private static void testComplexStructure() {
+ System.out.println("銆愭祴璇�4锛氬鏉傜粨鏋勩��");
+
+ // 鏋勯�犲鏉傜粨鏋�
+ JSONObject data = new JSONObject();
+ data.put("orderNumber", "PO004");
+ data.put("orderStatus", "PENDING");
+
+ // 瀹㈡埛淇℃伅
+ JSONObject customer = new JSONObject();
+ customer.put("customerCode", "CUST001");
+ customer.put("customerName", "鍖椾含鍏徃");
+ data.put("customer", customer);
+
+ // 璁㈠崟鏄庣粏鏁扮粍
+ JSONArray items = new JSONArray();
+ for (int i = 1; i <= 2; i++) {
+ JSONObject item = new JSONObject();
+ item.put("itemNo", i);
+ item.put("materialCode", "MAT00" + i);
+ item.put("materialName", "鐗╂枡" + i);
+ item.put("itemQty", 50 + i * 10);
+
+ // 浠撳簱淇℃伅锛堝祵濂楀湪鏄庣粏涓級
+ JSONObject warehouse = new JSONObject();
+ warehouse.put("warehouseCode", "WH0" + i);
+ warehouse.put("locationCode", "LOC-A-0" + i);
+ item.put("warehouse", warehouse);
+
+ items.add(item);
+ }
+ data.put("items", items);
+
+ System.out.println("鍘熷鏁版嵁锛�" + data.toJSONString());
+
+ // 瀹氫箟瀹屾暣鐨勬槧灏勮鍒�
+ Map<String, String> rules = new HashMap<>();
+ rules.put("orderNumber", "code");
+ rules.put("orderStatus", "exceStatus");
+ rules.put("customerCode", "custCode");
+ rules.put("customerName", "custName");
+ rules.put("materialCode", "matnr");
+ rules.put("materialName", "maktx");
+ rules.put("itemQty", "qty");
+ rules.put("warehouseCode", "whCode");
+ rules.put("locationCode", "locCode");
+
+ // 鎵ц閫掑綊鏇挎崲
+ JSONObject result = ParamsMapUtils.replaceJsonKeys(data, rules);
+
+ System.out.println("鏇挎崲缁撴灉锛�" + result.toJSONString());
+ System.out.println();
+ }
+}
--
Gitblit v1.9.1