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();
|
}
|
}
|