8

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

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't have a control on the json string. I can change the structure of the class though.
In that case try changing private Map<String, B> imageMaps; to private List<B> imageMaps;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.