Meaningless Drivel is fun!
The moose likes Other Open Source Projects and the fly likes JSON to HASHMAP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "JSON to HASHMAP" Watch "JSON to HASHMAP" New topic


Sanjeev MutalikDesai

Joined: Aug 25, 2005
Posts: 6
I have a json object in the following format:

"v1": {"ids": {"hotels":{"123":"456", "789":"012"}}},
"v2": {"ids": {"hotels":{"981":"123", "5412":"12"}}}

This is basically a hash of hash of hash object.

Is there a way to convert this json to java HashMap object i.e. I want the output as Hash of Hash of Hash..?

I tried using the json lib api's ( but couldn't do it.

Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
GSON supports it. Use Gson#fromJson().
Sanjeev MutalikDesai

Joined: Aug 25, 2005
Posts: 6
Thank you for the reply.

I tried following kind of code, and it works:

Gson g = new Gson();
Map<String, Map><String, String>> m4 = g.fromJson(sz,new TypeToken<Map><String, Map><String, String>>>(){}.getType());
Map<String, Map><String, String>> map = g.fromJson(jsonString, m4)

But problem here is, I need to know the entire HashMap depth level. Can I just get a HashMap object without knowing the depth level...something like:

retMap = gson.fromJson(json, Map),

so that I don't know whether its Map<String, Map><String, String>> or just Map<String, String>?

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63532

Moved to a more appropriate location.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: JSON to HASHMAP
It's not a secret anymore!