吉安感知网项目-后端
xiebin
2026-01-06 d207a86cdf1ab52ef8cb7cd83bad8fceab8038cf
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
package org.sxkj.common.utils;
 
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
 
import java.util.Iterator;
import java.util.Map;
 
public class CamelKeyToSnakeConverter {
    public static String formatKey(String json) {
        // 原始 JSON 字符串(驼峰命名)
 
        // 创建 ObjectMapper
        ObjectMapper mapper = new ObjectMapper();
 
        // 将 JSON 字符串转换为 JsonNode
        JsonNode rootNode = null;
        try {
            rootNode = mapper.readTree(json);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
 
        // 递归处理 JSON 键
        JsonNode convertedNode = convertKeysToSnakeCase(rootNode);
 
        // 将转换后的 JsonNode 重新序列化为 JSON 字符串
        String convertedJson = null;
        try {
            convertedJson = mapper.writeValueAsString(convertedNode);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        System.out.println(convertedJson);
        return convertedJson;
    }
 
    private static JsonNode convertKeysToSnakeCase(JsonNode node) {
        if (node.isObject()) {
            // 处理 JSON 对象
            ObjectNode objectNode = (ObjectNode) node;
            ObjectNode newObjectNode = objectNode.objectNode();
 
            Iterator<Map.Entry<String, JsonNode>> fields = objectNode.fields();
            while (fields.hasNext()) {
                Map.Entry<String, JsonNode> field = fields.next();
                String newKey = camelToSnakeCase(field.getKey()); // 转换键
                newObjectNode.set(newKey, convertKeysToSnakeCase(field.getValue())); // 递归处理值
            }
 
            return newObjectNode;
        } else if (node.isArray()) {
            // 处理 JSON 数组
            ArrayNode arrayNode = (ArrayNode) node;
            ArrayNode newArrayNode = arrayNode.arrayNode();
 
            for (JsonNode item : arrayNode) {
                newArrayNode.add(convertKeysToSnakeCase(item)); // 递归处理数组中的每个元素
            }
 
            return newArrayNode;
        }
 
        // 如果是其他类型(如字符串、数字等),直接返回
        return node;
    }
 
    private static String camelToSnakeCase(String camelCase) {
        StringBuilder snakeCase = new StringBuilder();
        for (char ch : camelCase.toCharArray()) {
            if (Character.isUpperCase(ch)) {
                snakeCase.append('_').append(Character.toLowerCase(ch));
            } else {
                snakeCase.append(ch);
            }
        }
        return snakeCase.toString();
    }
}