如何使用GSON反序列化Array中的Double数组?

i5desfxk  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(139)

我有一个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
    ]
  ]
}

我的对象类是这样的:

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文件时,我的数组字段是空的。双精度值的“no-name”数组让我很困惑,但似乎我的对象类应该在这里工作。该文件来自使用GSON工厂的一个改型调用。
编辑:
改装接口:

@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) {

        }
    });
}
rt4zxlrg

rt4zxlrg1#

此处必须使用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 + '}';
    }
}

您在此处的查询中使用了double而不是string作为时间戳(unix),因此请使用如下所示的string
第一个
请查看此屏幕截图

相关问题