简体   繁体   English

对象映射器反序列化映射数组

[英]Object Mapper deserializing map array

   {
       "name" : "test",
       "info": [
          {
            "key1": "test",
            "key2": "blaa",
            "key3": "yadayada"
          },
          {
            "key4": "test1",
            "key5": "blaa1",
            "key6": "yadayada1"
          }
        ],
    }

I have this class for the deserialization 我有这个班的反序列化

public class Account{
    private String name;
    private Map<String,String>[] info;
}

for some reason info is not getting deserialized... not even with List<Map<String,String>> its always null, and name is working 由于某种原因, info不会反序列化...即使使用List<Map<String,String>>也不总是反序列化,并且name始终为null

(im using ObjectMapper ) (即使用ObjectMapper

the code 编码

   ObjectMapper objectMapper = new ObjectMapper();
   objectMapper.disable( DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
   results = objectMapper.readValue(responseBody.getEntity().getContent(), Account.class);

Thanks 谢谢

The problem lies on the fact that the JSON schema you posted has a wrong comma in the end ) . 问题在于您发布的JSON模式末尾带有错误的逗号 )。 Try the following 尝试以下

Let's say that the responseBody.getEntity().getContent() is the JSON you posted 假设responseBody.getEntity().getContent()是您发布的JSON

Account.java Account.java

or more specific 或更具体

private String name;
private Map<String,String>[] info;
//getters && setters && default constructor

Deserialization method 反序列化方法

    Account account = null;
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        account = objectMapper.readValue(responseBody.getEntity().getContent(), Account.class);
    } catch (IOException e) {
        e.printStackTrace();
    }

This is a mixed bag of things wrong. 这是错杂的事情。

  1. Your JSON is invalid - ditch the last comma after the closing square bracket 您的JSON无效-在右方括号后使用最后一个逗号
  2. Your POJO shows no accessible methods to set the values (ie deserialize into an Account ) 您的POJO没有显示可访问的方法来设置值(即反序列化为Account
  3. According to your post, the name field is de-serialized, which I imagine implies some accessible setter is actually there but you may not have posted it? 根据您的帖子, name字段已反序列化,我想这实际上意味着确实存在一些可访问的设置器,但您可能还没有发布它?
  4. If the info silently fails to serialize, it's likely because your ObjectMapper has been configured with DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES as false - see docs . 如果info静静地失败序列化,因为你很可能ObjectMapper已经配置了DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIESfalse看到- 文档
  5. You can either have a List<Map<String, String>> for info , or keep the Map<String, String>[] - it's not relevant to your issue. 您可以使用List<Map<String, String>>作为info ,或保留Map<String, String>[] -与您的问题无关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM