简体   繁体   中英

Jackson: deserialize any json

This shouldn't be too difficult ... but I seem to be blind and don't find the answer myself.

Using Jackson I'd like to deserialize any ** JSON in a Java Map. The Array part is making the trouble.

any json could look like: { "foo": "bar" } or { "test": [ { "a": 123, "b": "erny" }, { "a": ... and so on

these should deserialize in (pseudocode):

Map( foo => "bar" )
Map( test => Array( 
    Map( a => 123, b=>erny ),
    Map( a => ... and so on
) )

I don't care much for the types of Arrays and Maps as long as they are that nor for the types of values. (But would be nice if integers were represented as such).

If I try om.readValue( json, Map.class ) I get Cannot deserialize instance of java.lang.String out of START_ARRAY error if the json contains an array.

I'm using Jackson quite successfully for all my complicated JSON stuff but how to do the simple one?

NOTE: Jackson does exactly what I want it to do using the syntax above. The problem was a bug in my json. So this question doesn't make sense.

**) any is not completely true. Every JSON I use is a map on its lowest level.

Jackson can deserialize JSON objects into java.util.Map and JSON arrays into java.util.List

From your error message it seems you are trying to deserialize a JSON array into a java.util.Map which is not possible.

@JsonAnyGetter public Map<String, Object> getOtherInfo() { return otherInfo; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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