Util工具类:
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import java.util.Map;
import java.util.stream.Collectors;
public class JsonNamingUtil {
/**
* 将 JSON 字符串中的下划线键名转换为驼峰键名
* @param jsonStr 下划线命名的 JSON 字符串
* @return 驼峰命名的 JSON 字符串
*/
public static String underlineToCamel(String jsonStr) {
JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
return convertKeys(jsonObject, JsonNamingUtil::underlineToCamelKey);
}
/**
* 将 JSON 字符串中的驼峰键名转换为下划线键名
* @param jsonStr 驼峰命名的 JSON 字符串
* @return 下划线命名的 JSON 字符串
*/
public static String camelToUnderline(String jsonStr) {
JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
return convertKeys(jsonObject, JsonNamingUtil::camelToUnderlineKey);
}
/**
* 将 Map 中的键名从驼峰转换为下划线
* @param map 驼峰命名的 Map
* @return 下划线命名的 Map
*/
public static Map<String, Object> camelMapToUnderline(Map<String, Object> map) {
return map.entrySet().stream()
.collect(Collectors.toMap(
e -> camelToUnderlineKey(e.getKey()),
Map.Entry::getValue
));
}
/**
* 将 Map 中的键名从下划线转换为驼峰
* @param map 下划线命名的 Map
* @return 驼峰命名的 Map
*/
public static Map<String, Object> underlineMapToCamel(Map<String, Object> map) {
return map.entrySet().stream()
.collect(Collectors.toMap(
e -> underlineToCamelKey(e.getKey()),
Map.Entry::getValue
));
}
// 核心转换方法
private static String convertKeys(JSONObject jsonObject, KeyConverter converter) {
JSONObject result = new JSONObject(true); // 保持顺序
for (String key : jsonObject.keySet()) {
String newKey = converter.convert(key);
Object value = jsonObject.get(key);
// 递归处理嵌套对象
if (value instanceof JSONObject) {
value = convertKeys((JSONObject) value, converter);
}
result.put(newKey, value);
}
return result.toString();
}
// 下划线转驼峰键名
private static String underlineToCamelKey(String key) {
return StrUtil.toCamelCase(key);
}
// 驼峰转下划线键名
private static String camelToUnderlineKey(String key) {
return StrUtil.toUnderlineCase(key);
}
// 键名转换函数接口
private interface KeyConverter {
String convert(String key);
}
}
Main方法测试:
public class TestJsonNaming {
public static void main(String[] args) {
// 示例JSON(下划线命名)
String underlineJson = "{\"user_name\":\"张三\",\"user_age\":25,\"contact_info\":{\"phone_number\":\"13800138000\"}}";
// 转换为驼峰命名
String camelJson = JsonNamingUtil.underlineToCamel(underlineJson);
System.out.println("驼峰命名JSON: " + camelJson);
// 输出: {"userName":"张三","userAge":25,"contactInfo":{"phoneNumber":"13800138000"}}
// 转换回下划线命名
String backToUnderline = JsonNamingUtil.camelToUnderline(camelJson);
System.out.println("下划线命名JSON: " + backToUnderline);
// 输出: {"user_name":"张三","user_age":25,"contact_info":{"phone_number":"13800138000"}}
// Map转换示例
Map<String, Object> camelMap = Map.of(
"userName", "李四",
"userAge", 30,
"contactInfo", Map.of("phoneNumber", "13900139000")
);
Map<String, Object> underlineMap = JsonNamingUtil.camelMapToUnderline(camelMap);
System.out.println("下划线Map: " + underlineMap);
// 输出: {user_name=李四, user_age=30, contact_info={phone_number=13900139000}}
}
}
JSON 键名转换:,underlineToCamel():将下划线命名的 JSON 转为驼峰命名,camelToUnderline():将驼峰命名的 JSON 转为下划线命名,支持嵌套 JSON 对象的递归转换,Map 键名转换:,camelMapToUnderline():转换 Map 的键名(驼峰→下划线),underlineMapToCamel():转换 Map 的键名(下划线→驼峰