图标库
This commit is contained in:
@ -41,7 +41,8 @@ public class JsonUtil {
|
||||
}
|
||||
|
||||
try {
|
||||
return objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {});
|
||||
return objectMapper.readValue(json, new TypeReference<Map<String, Object>>() {
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.error("JSON转Map失败、JSON内容: {}", json, e);
|
||||
return new HashMap<>(0);
|
||||
@ -67,7 +68,7 @@ public class JsonUtil {
|
||||
}
|
||||
|
||||
try {
|
||||
// 使用ObjectMapper将Map转换为指定类型对象
|
||||
// 使用 ObjectMapper 将 Map 转换为指定类型对象
|
||||
return objectMapper.convertValue(map, clazz);
|
||||
} catch (IllegalArgumentException e) {
|
||||
log.error("Map转对象失败、目标类型: {}, Map内容: {}", clazz.getName(), map, e);
|
||||
@ -86,4 +87,49 @@ public class JsonUtil {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改JSON字符串中指定键的值
|
||||
*/
|
||||
public static String modifyJsonValue(String json, String key, Object value) {
|
||||
if (json == null || json.trim().isEmpty() || key == null || key.trim().isEmpty()) {
|
||||
return json;
|
||||
}
|
||||
|
||||
// 将JSON转换为Map
|
||||
Map<String, Object> map = jsonToMap(json);
|
||||
if (map == null) {
|
||||
return json;
|
||||
}
|
||||
|
||||
// 解析键、支持嵌套
|
||||
String[] keyParts = key.split("\\.");
|
||||
Map<String, Object> currentMap = map;
|
||||
|
||||
// 遍历键的各个部分、处理嵌套结构
|
||||
for (int i = 0; i < keyParts.length; i++) {
|
||||
String part = keyParts[i];
|
||||
|
||||
// 如果是最后一个部分、直接设置值
|
||||
if (i == keyParts.length - 1) {
|
||||
currentMap.put(part, value);
|
||||
break;
|
||||
}
|
||||
|
||||
// 如果不是最后一个部分、检查是否存在该键且对应的值是Map
|
||||
Object nextObj = currentMap.get(part);
|
||||
if (nextObj instanceof Map) {
|
||||
currentMap = (Map<String, Object>) nextObj;
|
||||
} else {
|
||||
// 如果不存在或不是 Map、创建新的 Map
|
||||
Map<String, Object> newMap = new HashMap<>();
|
||||
currentMap.put(part, newMap);
|
||||
currentMap = newMap;
|
||||
}
|
||||
}
|
||||
|
||||
// 将修改后的 Map 转换回 JSON
|
||||
String result = mapToJson(map);
|
||||
return result != null ? result : json;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user