gpt4 book ai didi

java - GSON "Expected BEGIN_OBJECT but was BEGIN_ARRAY"错误

转载 作者:行者123 更新时间:2023-12-01 16:20:38 25 4
gpt4 key购买 nike

我是 json 解析的新手,最近我一直在使用 Retrofit 从服务器检索数据,如下所示:

[
[ time, low, high, open, close, volume ],
[ 1415398768, 0.32, 4.2, 0.35, 4.2, 12.3 ],
...
]

我的对象类是

import java.util.List;


public class cbProducts {
List<String> data;

public cbProducts(List<String> data) {
this.data = data;

}
}

我的界面

public interface cbClient {

@GET("/products/BTC-USD/candles")
Call<List<cbProducts>> Authenticate(
@Header("CB-ACCESS-KEY") String key,
@Header("CB-ACCESS-SIGN") String sign,
@Header("CB-ACCESS-TIMESTAMP") String timestamp,
@Header("CB-ACCESS-PASSPHRASE") String passphrase,
@Query("start") String startTime,
@Query("end") String endTime,
@Query("granularity") String granularity
) ;

}

我的回应

Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl("https://api.pro.coinbase.com")
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
cbClient client = retrofit.create(cbClient.class);

Call<List<cbProducts>> call = client.Authenticate("XXXX","XXXX","XXXX","XXXX","XXXX","XXXX","XXXX");

call.enqueue(new Callback<List<cbProducts>>() {
@Override
public void onResponse(Call<List<cbProducts>> call, Response<List<cbProducts>> response) {
//cbProducts[] cbP = new Gson().fromJson(String.valueOf(response), (Type) cbProducts[].class);
Gson gson = new Gson();
cbProducts cb = gson.fromJson(String.valueOf(response),cbProducts.class);
Log.d("MESSAGE",cb.toString());
Log.d("MESSAGE", String.valueOf(response.code()));


}

@Override
public void onFailure(Call<List<cbProducts>> call, Throwable t) {
Log.d("MESSAGE", t.getMessage());

}
});

我在读取预期是 BEGIN_OBJECT 但实际是 BEGIN_ARRAY 时不断收到相同的错误。我不明白为什么当我尝试接收数组时 java 需要一个 Json 对象。任何帮助将不胜感激。

最佳答案

不需要再次解析response,调用reponse.body()即可得到List<cbProducts>删除这个

Gson gson = new Gson();
cbProducts cb = gson.fromJson(String.valueOf(response),cbProducts.class);
Log.d("MESSAGE",cb.toString());
Log.d("MESSAGE", String.valueOf(response.code()));

并使用

List<cbProducts> products = response.body();

关于java - GSON "Expected BEGIN_OBJECT but was BEGIN_ARRAY"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62294129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com