1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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();
    }
}