Search code examples
javaobjectmapper

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

(im using ObjectMapper)

the code

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

Thanks


Solution

  • This is a mixed bag of things wrong.

    1. Your JSON is invalid - ditch the last comma after the closing square bracket
    2. Your POJO shows no accessible methods to set the values (i.e. deserialize into an 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?
    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.
    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.