【问题标题】:Expected BEGIN_OBJECT but was BEGIN_ARRAY but the json respone has already an object预期为 BEGIN_OBJECT 但为 BEGIN_ARRAY 但 json 响应已经有一个对象
【发布时间】:2021-06-13 06:36:26
【问题描述】:

我正在学习改造 2,但遇到这样的错误:

W/System.err: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 预期 BEGIN_OBJECT 但在第 1 行第 94 列路径 $.riceField 为 BEGIN_ARRAY 在 com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:226) 在 com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131) 在 com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222) 在 retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:40) 在 retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)

这是代码

API

@GET(Server.riceFields+"/{id}")
Call<Product> showRiceFields(@Path("id") int id);

型号

public class Product {

    private Status status;
    private RiceField riceField;

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public RiceField getRiceField() {
        return riceField;
    }

    public void setRiceField(RiceField riceField) {
        this.riceField = riceField;
    }


}

ProductsEndPoint.productsEndPoint().showRiceFields(id)
            .enqueue(new Callback<Product>() {
                @Override
                public void onResponse(Call<Product> call, Response<Product> response) {
                    if (response.isSuccessful()) {
                        RiceField riceField = response.body().getRiceField();
                        product = riceField;
                        setView(product);
                    }
                }

                @Override
                public void onFailure(Call<Product> call, Throwable t) {
                    t.printStackTrace();
                }
            });

json 响应

{
    "status": {
        "code": 200,
        "message": "Succes",
        "description": "Data berhasil diambil"
    },
    "riceField": [
        {
            "id": 21,
            "title": "Nihil qui placeat tempora magnam aut omnis quae alias aliqua Molestiae culpa",
            "harga": 45,
            "luas": 20,
            "alamat": "Ratione cum et aliqu",
            "maps": "Quia aut ut dolores nisi hic necessitatibus asperiores odio dolores ut non voluptatum ipsum enim id",
            "deskripsi": "Voluptatum repellend",
            "sertifikasi": "sgb",
            "tipe": "jual",
            "created_at": "2021-06-08T08:11:00.000000Z",
            "updated_at": "2021-06-08T08:11:00.000000Z",
            "user_id": 2,
            "vestige_id": 2,
            "irrigation_id": 3,
            "region_id": 3,
            "verification_id": 2
        }
    ]
}

我在回调中添加了 List 像这样&lt;List&lt;Product&gt;&gt; 并且错误转向

BEGIN_ARRAY 但为 BEGIN_OBJECT

那么,我该怎么办?

感谢和抱歉我的英语不好

解决方案:我应该在每个数组 json 的模型上添加 List

【问题讨论】:

    标签: android retrofit2


    【解决方案1】:

    您使用了错误的型号。根据您的 json 输出,您的模型必须是以下类

    public class Output{
    
    public Status status;
    public List<RiceField> riceField;
    
    }
    
    
    public class RiceField {
    
    public Integer id;
    public String title;
    public Integer harga;
    public Integer luas;
    public String alamat;
    public String maps;
    public String deskripsi;
    public String sertifikasi;
    public String tipe;
    public String createdAt;
    public String updatedAt;
    public Integer userId;
    public Integer vestigeId;
    public Integer irrigationId;
    public Integer regionId;
    public Integer verificationId;
    
    }
    
    
    public class Status {
    
    public Integer code;
    public String message;
    public String description;
    
    }
    

    欲了解更多信息,请使用此网站: json to java class

    【讨论】:

    • 非常感谢。所以我应该在每个数组响应的模型上添加列表
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 2015-07-24
    • 2014-08-01
    相关资源
    最近更新 更多