Madden Madden - 4 years ago 275
Java Question

Jackson: Cannot deserialize instance of object out of START_ARRAY

I'm getting this error when attempting to parse some JSON previously generated with Jackson. I generate the JSON like so

String ret = "";
ret = mapper.writeValueAsString(message.getPayload());
message.setPayload(ret);


Where message.getPayload() is a HashMap, in this instance containing two strings and a List of various objects. This creates the following malformed JSON

{
"user" : "john d example",
"items" : [ {
"val" : 99.5,
"id" : "phone",
"qty" : 1
}, {
"val" : 15.5,
"id" : "wine",
"qty" : 4
} ],
"address" : "123 example street"
}


Which throws an exception when examined thusly

Map<String, Object> ret = new HashMap<String, Object>();
String s = (String)message.getPayload();
ret = mapper.readValue(s, new TypeReference<Map<String, String>>(){});


How should I properly write this Map to JSON?

Answer Source

TypeReference<Map<String, String>> should be TypeReference<Map<String, Object>>. Jackson is attempting to parse the values as Strings rather than Lists because that is what it expects based on the TypeReference you passed in.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download