Ivor
Ivor

Reputation: 578

How to parse JsonObject List without knowing the key with Gson?

I'm having a bit trouble with parsing this JsonObject as I don't know the keys that will come and I'm using Gson. Here is an example of the json:

"calendar": {
         "201rj5-9": {
                "year": "2015",
                "month": "9",
         },
         "20tfh15-9": {
                "year": "2015",
                "month": "9",
         },
         [...] 
}

Upvotes: 1

Views: 1111

Answers (1)

Floern
Floern

Reputation: 33904

This looks like a Map, thus you could use a HashMap for your calendar field:

public class Root {
    HashMap<String, Entry> calendar;

    public static class Entry {
        String year, month;
    }
}

And just the matching Gson code for completeness:

Root root = new Gson().fromJson(json, Root.class);

Upvotes: 5

Related Questions