使用 retrofit2 解析 JSON 个对象失败

Parsing unsuccessfully JSON Objects with retrofit2

我在带有 gson 的代码中使用了 retrofit2。 我几乎可以解析所有需要的东西,但是在解析特定的 JSON 时,我遇到了问题。

这是JSON:

{
"data": [
{
"id": "001",
"direccion": "Cascada 3",
"provincia": "Burgos"
},
{
"id": "002",
"direccion": "Lago 21",
"provincia": "Zamora"
}
]
}

我知道我要写的东西有很多评论,但是请读到最后

解析时显示此错误:

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

如果我从JSON中删除对象“data”的名称,那么我可以完美地解析它,就像这样:

[
{
"id": "001",
"direccion": "Cascada 3",
"provincia": "Burgos"
},
{
"id": "002",
"direccion": "Lago 21",
"provincia": "Zamora"
}
]

JSON继续用对象打开,但是可以完美解析 这使我认为我没有在代码中指示如何解析名为 "data" 的对象,我认为问题在于。

Tiendas.java(我使用@serializedName 作为我已阅读但不起作用的可能解决方案)

import com.google.gson.annotations.SerializedName;

public class Tiendas {

    @SerializedName("id")
    private int id;
    @SerializedName("provincia")
    private String provincia;
    @SerializedName("direccion")
    private String direccion;

    public int getId() {
        return id;
    }
    public String getProvincia() {
        return provincia;
    }
    public String getDireccion() {
        return direccion;
    }


}

界面

 @GET("tiendas/tiends.json")
 Call<List<Tiendas>> getTiendas();

MainActivity.java

private void getGETsinParametros() {

     Retrofit retrofit = new Retrofit.Builder()
             .baseUrl("https://www.MY_URL_BASE.com/")
             .addConverterFactory(GsonConverterFactory.create())
             .build();

     JsonPlaceHolderApi jspAPI = retrofit.create(JsonPlaceHolderApi.class);

     Call<List<Tiendas>> call = jspAPI.getTiendas();

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

             if (!response.isSuccessful()) {

                 Log.e(TAG, "Código de respuesta no exítoso:" + response.code());

             } else {

                 Log.d(TAG,"response code:"+response.code());

                 List<Tiendas> listaObtenida = response.body();

                 for(Tiendas stores: listaObtenida){
                     String content = "";
                     content += "id" + stores.getId() + "\n";
                     content += "provincia" + stores.getProvincia() + "\n";
                     content += "direccion" + stores.getDireccion() + "\n";

                  jsonText.append(content);

                 }
             }

         }

         @Override
         public void onFailure(Call<List<Tiendas>> call, Throwable t) {
             Log.e("TAG, "error:" + t.getMessage());
         }
     });

 }

谁能告诉我如何使用 "data" 修复错误,或者如何修复错误?

JSON是外部的,所以我不能修改它们。

感谢和问候。

您需要为 Array 对象创建包装器以获取 tiendas 列表:

public class Data{

    private List<Tiendas> data;

    public List<Tiendas> getData() {
        return data;
    }

}

并在您的界面中调用它:

 @GET("tiendas/tiends.json")
 Call<Data> getTiendas();

不要忘记在 onResponse 中使用新对象:

List<Tiendas> listaObtenida = response.body().getData();

错误与Retrofit无关。对于一个JSON,一个field是一个对象,[]是一个数组,{}是一个POJO。

在您的第一个 JSON 中,您有一个类型为 List(或 Array)的 data 对象。因此,您需要在上面创建一个 POJO(为简单起见,省略了访问修饰符)

class DataClazz {
    List<Tiendas> data;
}