Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token

Random Coder picture Random Coder · Jun 25, 2018 · Viewed 14.7k times · Source

Below mentioned is the JSON string, resultString:

{
"imageMaps": [{
        "crc": "c2c4",
        "flags": "0",
        "length": "117384",
        "index": 1,
        "version": "1.1.90ea",
        "status": ""
    }, {
        "crc": "7601",
        "flags": "8",
        "length": "117592",
        "index": 2,
        "version": "1.1.90ed",
        "status": ""
    }],
    "complete": true,
    "nextBootImageVersion": "",
    "lastKnownGoodImageVersion": "1.1.90ed",
    "runningImageVersion": "1.1.90ed"
}

I want to get the same converted to the object of class A:

public class A {

    private boolean complete;

    private String message;

    private String lastKnownGoodImageVersion;

    private String nextBootImageVersion;

    private String runningImageVersion;

    private Map<String, B> imageMaps;

    private  List<B> images;

    private MacID macId;

}

I am trying to convert the json to object of class A using the code :

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);    
A a = objectMapper.readValue(resultString, A.class);

Code for class B is:

public static class B {
    public String version;
    public int flags; 
    public int crc; 
    public long length; 
    public String index;
    public String status;
}

But getting the exception :

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token

Answer

Konrad Botor picture Konrad Botor · Jun 25, 2018

You declared property imageMaps as a Map<String, B> in your class, but in your JSON imageMaps is an array of B. The deserialization should work if you change imageMaps to images in your JSON.