吉安感知网项目-后端
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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package org.sxkj.common.utils;
 
import com.alibaba.nacos.shaded.com.google.gson.FieldNamingPolicy;
import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.alibaba.nacos.shaded.com.google.gson.GsonBuilder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.core.io.ClassPathResource;
 
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map;
 
public class JsonUtils {
 
    private static final ObjectMapper objectMapper = new ObjectMapper();
 
 
    /**
     * json字符串转驼峰
     *
     * @param json String
     * @return String
     */
    public static String convertToCamelCaseJson(String json) {
        JsonNode jsonNode = null;
        try {
            jsonNode = objectMapper.readTree(json);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
 
        jsonNode = convertKeysToCamelCase(jsonNode);
 
        try {
            return objectMapper.writeValueAsString(jsonNode);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
 
 
    /**
     * JsonNode的key转驼峰
     *
     * @param jsonNode JsonNode
     * @return JsonNode
     */
    public static JsonNode convertKeysToCamelCase(JsonNode jsonNode) {
        if (jsonNode.isObject()) {
            ObjectNode objectNode = objectMapper.createObjectNode();
            jsonNode.fields().forEachRemaining(entry -> {
                objectNode.set(snakeToCamel(entry.getKey()), convertKeysToCamelCase(entry.getValue()));
            });
            return objectNode;
        }
 
        if (jsonNode.isArray()) {
            ArrayNode arrayNode = objectMapper.createArrayNode();
            jsonNode.elements().forEachRemaining(entry -> {
                arrayNode.add(convertKeysToCamelCase(entry));
            });
 
            return arrayNode;
        }
 
        return jsonNode;
    }
 
    public static JsonNode convertKeysToSnake(JsonNode jsonNode) {
        if (jsonNode.isObject()) {
            ObjectNode objectNode = objectMapper.createObjectNode();
            jsonNode.fields().forEachRemaining(entry -> {
                objectNode.set(camelToSnake(entry.getKey()), convertKeysToCamelCase(entry.getValue()));
            });
            return objectNode;
        }
 
        if (jsonNode.isArray()) {
            ArrayNode arrayNode = objectMapper.createArrayNode();
            jsonNode.elements().forEachRemaining(entry -> {
                arrayNode.add(convertKeysToSnake(entry));
            });
 
            return arrayNode;
        }
 
        return jsonNode;
    }
 
 
    /**
     * 下划线转驼峰
     *
     * @param key String
     * @return String
     */
    public static String snakeToCamel(String key) {
        if (key.indexOf("_") != 0) {
            String[] s = key.split("_");
            StringBuilder stringBuilder = new StringBuilder(s[0]);
            for (int i = 1; i < s.length; i++) {
                stringBuilder.append(s[i].substring(0, 1).toUpperCase()).
                    append(s[i].substring(1));
            }
            return stringBuilder.toString();
        }
 
        return key;
    }
 
 
    /**
     * 驼峰转下划线
     *
     * @param key String
     * @return String
     */
    public static String camelToSnake(String key) {
        int length = key.length();
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            char current = key.charAt(i);
            if (Character.isUpperCase(current)) {
                if (i > 0) {
                    sb.append('_');
                }
                sb.append(Character.toLowerCase(current));
                continue;
            }
            sb.append(current);
        }
        return sb.toString();
    }
 
 
    public static String toCamelCaseJson(String json) {
        // 原始 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);
        }
        return convertedJson;
    }
 
    private static JsonNode convertKeysToSnakeCase(JsonNode node) {
        if (node.isObject()) {
            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()) {
            for (int i = 0; i < node.size(); i++) {
                ((ObjectNode) node).set(Integer.toString(i), convertKeysToSnakeCase(node.get(i)));
            }
        }
 
        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();
    }
 
    /**
     * 根据路径获取文件中的json
     * @param path resource 路径
     * @return json数据
     */
    public static JsonNode loadJsonFile(String path) {
        try (InputStream inputStream = new ClassPathResource(path).getInputStream()) {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.readTree(inputStream);
        } catch (IOException e) {
 
            e.printStackTrace();
        }
        return null;
    }
 
 
 
}