user3075588 user3075588 - 3 months ago 49
JSON Question

Retrofit parse JSON dynamic keys

I'm a newbie in Retrofit and i have a question.

How to parse the Json below using retrofit

"data": {
"Aatrox": {
"id": 266,
"title": "a Espada Darkin",
"name": "Aatrox",
"key": "Aatrox"
"Thresh": {
"id": 412,
"title": "o GuardiĆ£o das Correntes",
"name": "Thresh",
"key": "Thresh"



You could make your model POJO contain a Map<String, Champion> to deserialize into, to deal with the dynamic keys.


public class ChampionData {
    public Map<String, Champion> data;
    public String type;
    public String version;

public class Champion {
    public int id;
    public String title;
    public String name;
    public String key;

I'm not familiar with Retrofit besides that, but as someone in the comments said, the deserializing is done by Gson:

public ChampionData champions = new Gson().fromJson(json, ChampionData.class);

So to build on to the answer someone else posted, you can then do the following, assuming you've added the GsonConverterFactory:

public interface API {
    Call<ChampionData> getChampionData();