我想将json通过jackson库转换为包含camelCase键的地图...说...
从
{
"SomeKey": "SomeValue",
"AnotherKey": "another value",
"InnerJson" : {"TheKey" : "TheValue"}
}
到此......
{
"someKey": "SomeValue",
"anotherKey": "another value",
"innerJson" : {"theKey" : "TheValue"}
}
我的代码......
public Map<String, Object> jsonToMap(String jsonString) throws IOException
{
ObjectMapper mapper=new ObjectMapper();
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
return mapper.readValue(jsonString,new TypeReference<Map<String, Object>>(){});
}
但是这不起作用......甚至其他的propertyNamingStrategy也无法在json上运行......例如...
{
"someKey": "SomeValue"
}
mapper.setPropertyNamingStrategy(new PropertyNamingStrategy.PascalCaseStrategy())
到
{
"SomeKey": "SomeValue"
}
如何通过jackson获取camelCase Map键名...或者我应该手动循环映射和转换键还是有其他方法???
提前致谢...
答案 0 :(得分:6)
当您使用map / dictionaries而不是将JSON数据绑定到POJO(与JSON数据匹配的显式Java类)时,属性命名策略不适用:
类PropertyNamingStrategy ...定义JSON属性的名称 (“外部名称”)源自POJO方法和字段的名称 (“内部名称”)
因此,您必须首先使用Jackson解析数据,然后迭代结果并转换密钥。
更改您的代码:
public Map<String, Object> jsonToMap(String jsonString) throws IOException
{
ObjectMapper mapper=new ObjectMapper();
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
Map<String, Object> map = mapper.readValue(jsonString,new TypeReference<Map<String, Object>>(){});
return convertMap(map);
}
并添加以下方法:
public String mapKey(String key) {
return Character.toLowerCase(key.charAt(0)) + key.substring(1);
}
public Map<String, Object> convertMap(Map<String, Object> map) {
Map<String, Object> result = new HashMap<String, Object>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
result.put(mapKey(key), convertValue(value));
}
return result;
}
public convertList(Lst<Object> list) {
List<Object> result = new ArrayList<Object>();
for (Object obj : list) {
result.add(convertValue(obj));
}
return result;
}
public Object covertValue(Object obj) {
if (obj instanceof Map<String, Object>) {
return convertMap((Map<String, Object>) obj);
} else if (obj instanceof List<Object>) {
return convertList((List<Object>) obj);
} else {
return obj;
}
}
答案 1 :(得分:3)
您始终可以迭代地图的键并更新它们。但是,如果您只对使用驼峰大小写密钥生成JSON感兴趣,可以考虑下面描述的方法。
您可以拥有自定义密钥序列化程序。将Map
实例序列化为JSON时将使用它:
public class CamelCaseKeySerializer extends JsonSerializer<String> {
@Override
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException {
String key = Character.toLowerCase(value.charAt(0)) + value.substring(1);
gen.writeFieldName(key);
}
}
然后执行以下操作:
String json = "{\"SomeKey\":\"SomeValue\",\"AnotherKey\":\"another value\",\"InnerJson\":"
+ "{\"TheKey\":\"TheValue\"}}";
SimpleModule simpleModule = new SimpleModule();
simpleModule.addKeySerializer(String.class, new CamelCaseKeySerializer());
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(simpleModule);
Map<String, Object> map = mapper.readValue(json,
new TypeReference<Map<String, Object>>() {});
String camelCaseJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(map);
输出将是:
{
"someKey" : "SomeValue",
"anotherKey" : "another value",
"innerJson" : {
"theKey" : "TheValue"
}
}
通过这种方法,Map
的密钥不会出现在驼峰的情况下。但它会给你想要的输出。
答案 2 :(得分:0)
以下内容将使用任何“大小写格式”的键将json转换为使用camelCased键:
/**
* Convert all property keys of the specified JSON to camelCase
*/
public static String toJsonWithCamelCasedKeys(String json) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new SimpleModule()
.addKeySerializer(String.class, new JsonSerializer<>() {
@Override
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
String key = CaseUtil.toCamelCase(value);
gen.writeFieldName(key);
}
})
);
try {
Map<String, Object> jsonMap = objectMapper.readValue(json, new TypeReference<>() {});
return objectMapper.writeValueAsString(jsonMap);
} catch (Exception e) {
throw new JsonException("Error transforming JSON", e);
}
}
...以及一个CaseUtil实现可能是这样的:
import java.util.Arrays;
import java.util.stream.Collectors;
public class CaseUtil {
public static String toCamelCase(String s) {
if (s == null) {
return null;
}
else if (s.isBlank()) {
return "";
}
return decapitaliseFirstLetter(
String.join("", Arrays.stream(s.split("[-_\\s]"))
.map(CaseUtil::capitaliseFirstLetter)
.collect(Collectors.toList()))
);
}
private static String capitaliseFirstLetter(String s) {
return (s.length() > 0)
? s.substring(0, 1).toUpperCase() + s.substring(1)
: s;
}
private static String decapitaliseFirstLetter(String s) {
return (s.length() > 0)
? s.substring(0, 1).toLowerCase() + s.substring(1)
: s;
}
}
单元测试:
@Test
void jsonWithMiscCasedPropKeys_shouldConvertKeyToCamelCase() throws Exception {
String inputJson =
"{\"kebab-prop\": \"kebab\"," +
"\"snake_prop\": \"snake\"," +
"\"PascalProp\": \"pascal\"," +
"\"camelCasedProp\": \"camel\"}";
String expectedJson =
"{\"kebabProp\": \"kebab\"," +
"\"snakeProp\": \"snake\"," +
"\"pascalProp\": \"pascal\"," +
"\"camelCasedProp\": \"camel\"}";
String actualJson = Json.toJsonWithCamelCasedKeys(inputJson);
JSONAssert.assertEquals(expectedJson, actualJson, JSONCompareMode.LENIENT);
}