如何使用 GSON 反序列化 Array of Double inside?

How can I de-serialize Array of Double inside of Array using GSON?

我有一个 JSON 文件:

{
  "prices": [
    [
      1635631832100,
      61607.43864571635
    ],
    [
      1635632085704,
      61575.780699431976
    ]
  ],
  "market_caps": [
    [
      1635631398809,
      1164158508809.9917
    ],
    [
      1635631832100,
      1164158508809.9917
    ],
    [
      1635632085704,
      1164158508809.9917
    ]
  ],
  "total_volumes": [
    [
      1635632420811,
      30767786519.758457
    ],
    [
      1635632594220,
      30875566056.458145
    ],
    [
      1635632959263,
      30967148014.50128
    ],
    [
      1635633219013,
      30718683632.270718
    ]
  ]
}

我的对象class是这样的:

public class HistoricalPrices {
    
    private List<List<Double>> prices;
    private List<List<Double>> market_caps;
    private List<List<Double>> total_volumes;

    public List<List<Double>> getPrices() {
        return prices;
    }

    public List<List<Double>> getMarket_caps() {
        return market_caps;
    }

    public List<List<Double>> getTotal_volumes() {
        return total_volumes;
    }
}

我不确定我在这里做错了什么,因为当我尝试反序列化 JSON 文件时,我的数组字段是空的。 Double 值的“无名”数组让我失望,但我的对象 class 似乎应该在这里工作。该文件来自使用 GSON Factory 的改造调用。

编辑:

改装界面:

@GET("coins/{id}/market_chart/range")
Call<HistoricalPrices> getHistoricalPrices(
        @Path("id") String id, @Query("vs_currency")String currency, @Query("from") double startDate, @Query("to") double endDate);

改造调用:

private void populateHistoricalPrices() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://api.coingecko.com/api/v3/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    CoinGeckoApi coinGeckoApi = retrofit.create(CoinGeckoApi.class);
    Call<HistoricalPrices> call = coinGeckoApi.getHistoricalPrices("bitcoin", "usd", 1635597419, 1635633419);

    call.enqueue(new Callback<HistoricalPrices>() {
        @Override
        public void onResponse(Call<HistoricalPrices> call, Response<HistoricalPrices> response) {

            if(!response.isSuccessful()){
                //need to display response error
                return;
            }

            TextView textView = ((Activity) context).findViewById(R.id.mainTextView);
            textView.append(response.body().toString());
            HistoricalPrices historicalPrices = response.body();

        }

        @Override
        public void onFailure(Call<HistoricalPrices> call, Throwable t) {

        }
    });
}
    

你必须在这里使用 String 而不是 Long

public class HistoricalPrices {

    @SerializedName("prices")
    private List<List<String>> prices = null;
    @SerializedName("market_caps")
    private List<List<String>> marketCaps = null;
    @SerializedName("total_volumes")
    private List<List<String>> totalVolumes = null;

    public List<List<String>> getPrices() {
        return prices;
    }

    public void setPrices(List<List<String>> prices) {
        this.prices = prices;
    }

    public List<List<String>> getMarketCaps() {
        return marketCaps;
    }

    public void setMarketCaps(List<List<String>> marketCaps) {
        this.marketCaps = marketCaps;
    }

    public List<List<String>> getTotalVolumes() {
        return totalVolumes;
    }

    public void setTotalVolumes(List<List<String>> totalVolumes) {
        this.totalVolumes = totalVolumes;
    }

    @Override
    public String toString() {
        return "HistoricalPrices{" + "prices=" + prices + ", marketCaps=" + marketCaps + ", totalVolumes=" + totalVolumes + '}';
    }
}

并且您在查询中使用双精度而不是字符串作为时间戳 (unix) 在这里所以请使用这样的字符串

    @GET("coins/{id}/market_chart/range")
Call<HistoricalPrices> getHistoricalPrices(
        @Path("id") String id, @Query("vs_currency")String currency, @Query("from") String startDate, @Query("to") String endDate);

    Call<HistoricalPrices> call = coinGeckoApi.getHistoricalPrices("bitcoin", "USD", "1635597419", "1635633419");

请查看此屏幕截图