我有一個 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 檔案時,我的陣列欄位為空。Double 值的“無名”陣列讓我失望,但似乎我的物件類應該在這里作業。該檔案來自使用 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) {
}
});
}
uj5u.com熱心網友回復:
你必須在這里使用 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 而不是字串作為時間戳(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");
請檢查此螢屏截圖
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/343654.html
上一篇:按鍵截取值的迭代函式
