java - 读取 JSON - 改造 - Android Studio
问题描述
我真的需要帮助。我无法读取 JSON,我不知道我做错了什么。
我将把我的代码放在这里。
我有这个 Json
{
"id": "288",
"name": "Tarjeta Shopping",
"secure_thumbnail": "https://www.mercadopago.com/org-img/MP3/API/logos/288.gif",
"thumbnail": "http://img.mlstatic.com/org-img/MP3/API/logos/288.gif",
"processing_mode": "aggregator",
"merchant_account_id": null
}
这是我的类,应该代表那个 JSON
public class Tarjeta {
@SerializedName("id")
@Expose
private String id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("secure_thumbnail")
@Expose
private String secureThumbnail;
@SerializedName("thumbnail")
@Expose
private String thumbnail;
@SerializedName("processing_mode")
@Expose
private String processingMode;
@SerializedName("merchant_account_id")
@Expose
private Object merchantAccountId;
public Tarjeta() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSecureThumbnail() {
return secureThumbnail;
}
public void setSecureThumbnail(String secureThumbnail) {
this.secureThumbnail = secureThumbnail;
}
public String getThumbnail() {
return thumbnail;
}
public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}
public String getProcessingMode() {
return processingMode;
}
public void setProcessingMode(String processingMode) {
this.processingMode = processingMode;
}
public Object getMerchantAccountId() {
return merchantAccountId;
}
public void setMerchantAccountId(Object merchantAccountId) {
this.merchantAccountId = merchantAccountId;
}
@Override
public String toString() {
return "Tarjeta{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", secureThumbnail='" + secureThumbnail + '\'' +
", thumbnail='" + thumbnail + '\'' +
", processingMode='" + processingMode + '\'' +
", merchantAccountId=" + merchantAccountId +
'}';
}
}
这是我的 GET 方法
@GET("payment_methods/card_issuers")
Call<Tarjeta> getTarjetas2(@Query("public_key") String apiKey,
@Query("payment_method_id") String payment_method_id);
这就是我尝试阅读的地方。
botonTest2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("Test boton 2 clickeado");
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ServicePago servicePago = retrofit.create(ServicePago.class);
Call<Tarjeta> contenedorTarjetaCall = servicePago.getTarjetas2(apiKey,"visa");
contenedorTarjetaCall.enqueue(new Callback<Tarjeta>() {
@Override
public void onResponse(Call<Tarjeta> call, Response<Tarjeta> response) {
Toast.makeText(MainActivity.this, "BIEN", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<Tarjeta> call, Throwable t) {
Toast.makeText(MainActivity.this, "ALGO SALIO MAL", Toast.LENGTH_SHORT).show();
}
});
}
});
我遇到了这个错误:java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
我认为我的课程是正确建模的,我完全迷失了。
解决方案
由于您没有发布整个 JSON,我将用一个粗略的想法来回答,希望对您有所帮助。
该错误表明收到的 JSON 不是 Tarjeta,而是 Tarjeta 数组。所以要修复它,我想你只需要将你的回复包装在一个列表类型中。所以它是这样的:
@GET("payment_methods/card_issuers")
Call<List<Tarjeta>> getTarjetas2(@Query("public_key") String apiKey,
@Query("payment_method_id") String payment_method_id);
推荐阅读
- excel - PPT中的链接Excel图表(更新问题)
- javascript - 我想在 Nuxt.js 的整个视图中显示一个半透明背景作为组件公开
- c++ - (C++) docker 容器中的语言服务器
- c - 如何在 Win32 中以编程方式关闭对话框
- javascript - 为什么使用 HttpParams 对象在角度请求中使用查询参数?
- android - 有什么办法可以禁止在模拟器上截屏
- cakephp-3.0 - 个人资料图片未自动更新
- github - 如何保存 Jupyter html 输出的图像快照?
- android-studio - Android 项目同步失败
- javascript - react-swipable-views 使用时显示错误