Reconfiguring JSON Parsing

I am new to Retrofit. How to parse Json below using a modification?

{
   "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"
       }
   },
   "type":"champion",
   "version":"6.23.1"
}
+4
source share
2 answers

You can make your POJO model contain Map<String, Champion>deserialization to handle dynamic keys.

Example:

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 am not familiar with Retrofit yet, but as someone said in the comments, Gson deserializes:

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

So, to respond to a response sent by another user, you can do the following: if you added GsonConverterFactory:

public interface API {
    @GET("path/to/endpoint")
    Call<ChampionData> getChampionData();
}
+8
source

Retrofit2, , , Retrofit.

addConverterFactory(GsonConverterFactory.create())

POJO (, MyPojoClass), json, - Retrofit.

Call<MyPojoClass> makeRequest(<some params>);

+1

Source: https://habr.com/ru/post/1662453/


All Articles