1

This is the structure of the JSON that I need to transform:

{
"United Kingdom": {
    "visit_count": 2,
    "cities": {
        "London": 2
    }
},
"Netherlands": {
    "visit_count": 1182,
    "cities": {
        "Amsterdam": 441
    }
}
}

Which is basically a JSonObject, which contains an array of objects, BUT the key is the name of the country, and the right part, are the properties of the object. The same goes for the "Cities" JsonObject. Now I tried doing this with jsonschema2pojo, but it tries to create objects after the name of the countries (United Kingdom, Netherlads) when this objects are actually the same type. I was thinking of somehow loading the json into a hashmap, but don't know how exactly to do that. Is is possible?

rosu alin
  • 5,674
  • 11
  • 69
  • 150

2 Answers2

2

Use google's Gson, it works excellent.

https://code.google.com/p/google-gson/

And take a look into here

Community
  • 1
  • 1
Jorge Sanchez
  • 1,619
  • 1
  • 13
  • 14
  • I was using Gson, but didn't know I can load a gson structure like that inside a map. Usually I would make the Object Classes with http://www.jsonschema2pojo.org/ and then use GSon to load those classes, similar with how you did here with Map.class. But this helped me resolve my issue, thanks a lot!! – rosu alin Apr 13 '15 at 15:18
1

You can try to use JSON-simple library: https://code.google.com/p/json-simple/

JSONObjects parsed by this library are literally represented as a HashMap. (org.json.simple.JSONObject extends HashMap) :-)

Berťák
  • 7,143
  • 2
  • 29
  • 38