杰克逊JSON映射和驼峰键名 [英] Jackson json to map and camelcase key name

查看:88
本文介绍了杰克逊JSON映射和驼峰键名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to convert json via jackson library to a map containing camelCase key...say...

来自

{
    "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>>(){});
}

但是这不起作用...甚至其他属性NamingStrategy也不能在json上起作用...例如...

But this doesn't work...even other propertyNamingStrategy does not work on json...such as...

{
    "someKey": "SomeValue"
}

mapper.setPropertyNamingStrategy(new PropertyNamingStrategy.PascalCaseStrategy())

{
    "SomeKey": "SomeValue"
}

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

预先感谢...

推荐答案

在使用地图/字典时,而不是将JSON数据绑定到POJO(与JSON数据匹配的显式Java类)时,属性命名策略不会申请:

As you are working with maps/dictionaries instead of binding the JSON data to POJOs (explicit Java classes that match the JSON data), the property naming strategy does not apply:

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

Class PropertyNamingStrategy ... defines how names of JSON properties ("external names") are derived from names of POJO methods and fields ("internal names")

因此,您必须首先使用Jackson解析数据,然后遍历结果并转换密钥.

Therefore, you have to first parse the data using Jackson and then iterate over the result and convert the keys.

像这样更改代码:

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;
    }
}

这篇关于杰克逊JSON映射和驼峰键名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆