This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "JSON to HASHMAP" Watch "JSON to HASHMAP" New topic
Author

JSON to HASHMAP

Sanjeev MutalikDesai
Greenhorn

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 (http://json-lib.sourceforge.net/) but couldn't do it.


Bauke Scholtz
Ranch Hand

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

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
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

Moved to a more appropriate location.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JSON to HASHMAP