杰克逊JSON - 反编译Commons MultiMap [英] Jackson JSON - Deserialize Commons MultiMap

查看:200
本文介绍了杰克逊JSON - 反编译Commons MultiMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JSON序列化和反序列化MultiMap(Apache Commons 4)。

i want to serialize and deserialize a MultiMap (Apache Commons 4) using JSON.

要测试的代码片段:

MultiMap<String, String> map = new MultiValueMap<>();
map.put("Key 1", "Val 11");
map.put("Key 1", "Val 12");
map.put("Key 2", "Val 21");
map.put("Key 2", "Val 22");

ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(map);
MultiMap<String, String> deserializedMap = mapper.readValue(jsonString, MultiValueMap.class);

序列化工作正常并产生我期望的格式:

The serialization works fine and results in a format I would expect:

{"Key 1":["Val 11","Val 12"],"Key 2":["Val 21","Val 22"]}

不幸的是,反序列化产生的结果不是它的样子:
在反序列化之后,Multimap在一个键的值中包含一个 ArrayList 中的ArrayList,而不是包含值的键的单个ArrayList。

Unfortunately the deserialization produces a result that is not the way it should look like: After the deserialization, the Multimap contains an ArrayList inside an ArrayList for the values of a key, not an single ArrayList for the key containing the values.

由于MultiMap实现Map接口。

This result is produced due to the fact that the put() method of the multi map is called to add the array found in the json string, as the MultiMap implements the Map interface.

如果将新值放入非现有密钥,MultiMap实现本身会再创建一个ArrayList。

The MultiMap implementation itself again then creates an ArrayList if a new value is put to a non existing key.

有没有办法绕过这个?

感谢您的帮助!

推荐答案

作为从牛津词典中找到了绕过寻找方法(障碍)的方法,这是一个简单的解决方法。

Being assured from Oxford dictionary that circumvent means to "find a way around (an obstacle)", here is a simple work around.

首先我创建了一个生成相同的方法MultiValueMap如上所述。我使用相同的方法将其解析为json字符串。

First I created a method that generate the same MultiValueMap as yours above. And I use the same approach to parse it as a json string.

然后我创建了以下反序列化方法

I then created the following deserialization method

public static MultiMap<String,String> doDeserialization(String serializedString) throws JsonParseException, JsonMappingException, IOException {

    ObjectMapper mapper = new ObjectMapper();
    Class<MultiValueMap> classz = MultiValueMap.class;
    MultiMap map = mapper.readValue(serializedString, classz);
    return (MultiMap<String, String>) map;


}

当然这完全属于确切的你上面提到的问题,因此我创建了 doDeserializationAndFormat 方法:它将遍历每个列表中的列表,对应于给定的键并逐个将值关联到key

Of course this alone falls in the exact issue you mentionned above, therefore I created the doDeserializationAndFormatmethod: it will iterate through each "list inside a list" correponding to a given key and associate one by one the values to the key

public static MultiMap<String, String> doDeserializationAndFormat(String serializedString) throws JsonParseException, JsonMappingException, IOException {
    MultiMap<String, String> source = doDeserialization(serializedString);
    MultiMap<String, String> result  =  new MultiValueMap<String,String>();
    for (String key: source.keySet()) {


        List allValues = (List)source.get(key);
        Iterator iter = allValues.iterator();

        while (iter.hasNext()) {
            List<String> datas = (List<String>)iter.next();

            for (String s: datas) {
                result.put(key, s);
            }
        }

    }

    return result;

}

这是一个主要方法中的简单调用:

Here is a simple call in a main method:

MultiValueMap<String,String> userParsedMap = (MultiValueMap)doDeserializationAndFormat(stackMapSerialized);
System.out.println("Key 1 = " + userParsedMap.get("Key 1") );
System.out.println("Key 2 = " + userParsedMap.get("Key 2") );

希望这有帮助。

这篇关于杰克逊JSON - 反编译Commons MultiMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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