Question

I have a json in a specific form that I have to deserialize. In order to do that, I thought at my very best library Gson, but I'm facing a problem here because I have some key that are dynamic.

I think you will understand with a good exemple. Here is the json :

{

"institution1": {
    "_id": "51cc7bdc544ddb3f94000002",
    "announce": { },
    "city": "Melun",
    "coordinates": [
        2.65106,
        48.528976
    ],
    "country": "France",
    "created_at": "2013-06-27T17:52:28Z",
    "name": "italien",
    "state": "Seine et Marne",
    "street": "Avenue Albert Moreau",
    "updated_at": "2013-06-27T17:52:28Z"
},
"institution2": {
    "_id": "51d1dfa8544ddb9157000001",
    "announce": {
        "announce1": {
            "_id": "51d1e036544ddb9157000002",
            "created_at": "2013-07-01T20:02:35Z",
            "description": "description formule 1",
            "institution_id": "51d1dfa8544ddb9157000001",
            "title": "formule dejeune",
            "type": "restoration",
            "updated_at": "2013-07-01T20:02:35Z"
        },
        "announce2": {
            "_id": "51d1e074544ddb9157000003",
            "created_at": "2013-07-01T20:03:08Z",
            "description": "description formule soir",
            "institution_id": "51d1dfa8544ddb9157000001",
            "title": "formule soiree",
            "type": "restoration",
            "updated_at": "2013-07-01T20:03:08Z"
        }
    },
    "city": "Melun",
    "coordinates": [
        2.65106,
        48.528976
    ],
    "country": "France",
    "created_at": "2013-07-01T19:59:36Z",
    "name": "restaurant francais chez pierre",
    "state": "Seine et Marne",
    "street": "Avenue Albert Moreau",
    "updated_at": "2013-07-01T19:59:36Z"
 }
}

You can go here for a better view. So, I created a class to do that, JsonModel.java

public class JsonModel
{
public HashMap<String, Institution> entries;

public static class Institution
{
    public String _id;
    public HashMap<String, Announce> announce;
    public String city;
    public String country;
    public List<Double> coordinates;
    public String created_at;
    public String name;
    public String state;
    public String street;
    public String updated_at;
}

public static class Announce
{
    public String _id;
    public String created_at;
    public String description;
    public String institution_id;
    public String title;
    public String type;
    public String updated_at;
}

}

And then, I asked Gson to deserialize that with the following code :

JsonModel data = new Gson().fromJson(json, JsonModel.class);

As data is null I presume that it is not working the way I expected... And I thought about using HashMap because I don't know in advance the key like institution1, institution2, ... What do you think ? Can I do that ?

And please don't tell me to use bracket, I just dream to have those !

Thank you in advance !

EDIT : I was able to make this thing work by adding a root object

{

  "root":{ ..the json.. }

}

AND changing

    public HashMap<String, Institution> entries;

by

    public HashMap<String, Institution> root;

So, the problem is gson need to recognise the first element but in fact I will not be able to modify the json, is there a way to get it done differently ?

Was it helpful?

Solution

The use of a Map is perfect, but you can't use your JsonModel class, because with that class you're assuming that in your JSON you have an object that contains a field called "entries" that in turn represents a map, like this:

{
  "entries": { the map here... }
}

And you don't have that, but your JSON represents directly a map (not an object with a field called entries that represents a map!). Namely, you have only this:

{ the map here... }

So you need to parse it accordingly, removing the class JsonModel and using directly a Map to deserialize...

You need to use a TypeToken to get the type of your Map, like this:

Type mapType = new TypeToken<Map<String, Institution>>() {}.getType();

And then use the method .fromJson() with that type, like this:

Map<String, Institution> map = gson.fromJson(json, mapType);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top