Generically Deserializing Nested JSON Objects

480 views
Skip to first unread message

Daniel Carleton

unread,
Jan 18, 2010, 9:32:09 PM1/18/10
to google-gson
Hello List,

I'm deserializing a JSON document to a Map using the generics
support. I'd like any nested JSON objects in the document to also be
deserialized to a Map, creating a Map of Maps.

The trouble is that the parser balks on not having type information
for nested objects, and I'm not sure how to supply it:

Map doc = gson.fromJson("{"foo": {"bar": 1}}",
new TypeToken<Map<String, Object>>(){}.getType());

java.lang.RuntimeException: com.google.gson.JsonParseException: Type
information is unavailable, and the target object is not a primitive:
{"bar": 1}

Thanks for any advice, and for creating Gson!

Cheers,

- Daniel

Arun Shanker Prasad

unread,
Jan 19, 2010, 9:47:01 AM1/19/10
to google-gson
Hi,

I have a similar question, I need to get a Map from a generic JSON
Object, I thought using the JsonObject would help.

All I need is to convert something like;

{value1 : "value 1", value2 : ["1", "2"] }

to a Map is it possible? Without knowing the bean type I want to map
it to?

Thanks,
Arun Shanker Prasad.

On Jan 19, 7:32 am, Daniel Carleton <daniel.carle...@exaptic.com>
wrote:

Reply all
Reply to author
Forward
0 new messages