• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

JSON to HASHMAP

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
GSON supports it. Use Gson#fromJson().
 
Sanjeev MutalikDesai
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>?


 
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to a more appropriate location.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic