杰克逊杰森以地图和camelcase为主角

Sha*_*kil 8 java json jackson

我想将json通过jackson库转换为包含camelCase键的地图......说...

{
    "SomeKey": "SomeValue",
    "AnotherKey": "another value",
    "InnerJson" : {"TheKey" : "TheValue"}
}
Run Code Online (Sandbox Code Playgroud)

对...

{
    "someKey": "SomeValue",
    "anotherKey": "another value",
    "innerJson" : {"theKey" : "TheValue"}
}
Run Code Online (Sandbox Code Playgroud)

我的代码......

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>>(){});
}
Run Code Online (Sandbox Code Playgroud)

但是这不起作用......甚至其他的propertyNamingStrategy也无法在json上运行...比如......

{
    "someKey": "SomeValue"
}

mapper.setPropertyNamingStrategy(new PropertyNamingStrategy.PascalCaseStrategy())
Run Code Online (Sandbox Code Playgroud)

{
    "SomeKey": "SomeValue"
}
Run Code Online (Sandbox Code Playgroud)

如何通过jackson获取camelCase Map键名...或者我应该手动循环映射和转换键还是有其他方法???

提前致谢...

Cod*_*odo 6

当您使用map/dictionaries而不是将JSON数据绑定到POJO(与JSON数据匹配的显式Java类)时,属性命名策略不适用:

类PropertyNamingStrategy ...定义如何从POJO方法和字段的名称("内部名称")派生JSON属性的名称("外部名称")

因此,您必须首先使用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);
}
Run Code Online (Sandbox Code Playgroud)

并添加以下方法:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)


cas*_*lin 6

您始终可以遍历地图的键并更新它们。但是,如果您只对使用驼峰式大小写键生成 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后做如下操作:

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);
Run Code Online (Sandbox Code Playgroud)

输出将是:

{
  "someKey" : "SomeValue",
  "anotherKey" : "another value",
  "innerJson" : {
    "theKey" : "TheValue"
  }
}
Run Code Online (Sandbox Code Playgroud)

使用这种方法, 的键Map不会是驼峰式的。但它会给你想要的输出。