简体   繁体   中英

Retrofit,Dynamic json whose fields change depending on the parameters into Gson

I'm using retrofit2 and I need to call an API for information. The JSON that the API gives me has dynamic fields which change depending on the parameter values. Note that I can have any number of parameters. Below is the JSON for the parameters "1","2".

{
   "data":{
      "1":{

         "logo":"https://s2.coinmarketcap.com/static/img/coins/64x64/1.png",
         "id":1,
         "name":"Bitcoin",
         "symbol":"BTC",
         "description":"Bitcoin (BTC) is...",
         "date_added":"2013-04-28T00:00:00.000Z",
         "platform":null,
         "category":"coin"
      }
   },
   "2":{
      "logo":"https://s2.coinmarketcap.com/static/img/coins/64x64/1.png",
      "id":1,
      "name":"Bitcoin",
      "symbol":"BTC",
      "description":"Bitcoin (BTC) is...",
      "date_added":"2013-04-28T00:00:00.000Z",
      "platform":null,
      "category":"coin"
   }
}

I've created an interface in which I get the header and the parameter key which then I call later in retrofit to return me the info I need.

public interface CurrencyService {
    @GET(ApiConstants.CRYPTOCURRENCYINFO)
    Call<CurrencyService> getCurrencyById(@Header("X-CMC_PRO_API_KEY") 
String appkey,
                                          @Query("id") int[] ints);
}
public class CurrencyService {
    Map<String, Data> data;
}

public class Data {
    String logo;
    int id;
    String name;
    String symbol;
    String description;
    String date_added;
    Object platform;
    String category;
}

Call<CurrencyService> getCurrencyById(@Header("X-CMC_PRO_API_KEY")String appkey, @Query("id") int[] ints);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM