• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSON to HASHMAP

 
Sanjeev MutalikDesai
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
GSON supports it. Use Gson#fromJson().
 
Sanjeev MutalikDesai
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to a more appropriate location.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic