zundi
zundi

Reputation: 2457

Store all JSON in Map using gson and Retrofit 2

I'm getting a JSON value from an API using Retrofit. However since the actual keys are going to be different each time I request the data, I'd like to store everything in a Map. Is this possible using Gson as the parser?

For example: Access #1:

{
    "name": "Toby",
    "color": "blue"
}

Access #2:

{
    "game": "Soccer",
    "day": "Monday"
}

Instead of creating a class such as

class MyValues {
    public String name;
    public String color;
}

which would be impossible since the keys will always be different, I'd like something like:

class MyValues {
    public Map<String, String> myMap;
}

Thanks!

Upvotes: 4

Views: 2887

Answers (1)

iagreen
iagreen

Reputation: 32026

Yes, you don't need the MyValues class, you can use just Map<String, String> as the parameter type for your retrofit Call. --

@GET("/")
Call<Map<String, String>> myCall();

Upvotes: 5

Related Questions