预期 BEGIN ARRAY 但在第 1 行和第 2 列为 BEGIN_OBJECT

Expected BEGIN ARRAY but was BEGIN_OBJECT at line 1 and colum2

这是来自 api 的 json 数组数据/json 对象数据,预计会出现错误 begin_array 但 begin_object 在第 3 行和第 2 列路径$

这是来自 api 的 json 数组数据/json 对象数据,预计会出现错误 begin_array 但 begin_object 在第 3 行和第 2 列路径$

这是来自 api 的 json 数组数据/json 对象数据,预计会出现错误 begin_array 但 begin_object 在第 3 行和第 2 列路径$

这是来自 api 的 json 数组数据/json 对象数据,预计会出现错误 begin_array 但 begin_object 在第 3 行和第 2 列路径$

    "offersdata": [
        {
            "offercode": "GRAB20",
            "title": "Get Upto",
            "description": "20% off on selected merchandise on purchase of INR 1000 or more"
        },
        {
            "offercode": "JAN20",
            "title": "Get Upto",
            "description": "20% Off on all purchases in January"
        },
        {
            "offercode": "BHHH",
            "title": "DES",
            "description": "FDSFS"
        }
    ],
    "message": "success"
}
界面
 @GET("user")
    Call<List<limitedoffers>>  getoffers();
POJO CLASS
package com.example.medico.models;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class limitedoffers {

    @SerializedName("offersdata")
    @Expose
    private List<Offersdatum> offersdata = null;
    @SerializedName("message")
    @Expose
    private String message;

    public List<Offersdatum> getOffersdata() {
        return offersdata;
    }

    public void setOffersdata(List<Offersdatum> offersdata) {
        this.offersdata = offersdata;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

}

package com.example.medico.models;


import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;


public class Offersdatum {

    @SerializedName("offercode")
    @Expose
    private String offercode;
    @SerializedName("title")
    @Expose
    private String title;
    @SerializedName("description")
    @Expose
    private String description;

    public String getOffercode() {
        return offercode;
    }

    public void setOffercode(String offercode) {
        this.offercode = offercode;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

}

啊,这是我最喜欢的一期...

要解决此问题,您必须实施 serializer

对于你的情况,你必须自己将即将到来的对象转换为数组

我已经完全阅读了 Retrofit,当数组被完美序列化时,Retrofit 将起作用。

 "offersdata": [
        {
            "offercode": "GRAB20",
            "title": "Get Upto",
            "description": "20% off on selected merchandise on purchase of INR 1000 or more"
        },
        {
            "offercode": "JAN20",
            "title": "Get Upto",
            "description": "20% Off on all purchases in January"
        },
        {
            "offercode": "BHHH",
            "title": "DES",
            "description": "FDSFS"
        }
    ],
    "message": "success"
}

在上面的Jsonarray offersdata中有ArrayList

在接口 class 中必须更改此代码

@GET("user")
Call<List<limitedoffers>>  getoffers();

至此

@GET("user")
Call<limitedoffers>  getoffers();

因为它不包含ArrayList

用于在响应后从 Limitedoffers 获取数组只需使用

response.body().getOffersdata();

获取 offersdata Arraylist