大家好,我有一个类似this的api,您可以看到我有一个metaData数组,并且数组中的所有项目都有一个Integer ID和一个字符串键,但它们中的值都不相同在对象中定义了值,但是我有一个错误,这是错误
Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 3871 path $[0].meta_data
这是我的POJO课
主要Pojo(产品)类
@SerializedName("meta_data")
@Expose
private MetaDatum metaData;
这是MetaDatum类
public class MetaDatum implements Parcelable {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("key")
@Expose
private String key;
@SerializedName("value")
@Expose
private Object value;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(this.id);
dest.writeString(this.key);
dest.writeParcelable((Parcelable) this.value, flags);
}
public MetaDatum() {
}
protected MetaDatum(Parcel in) {
this.id = (Integer) in.readValue(Integer.class.getClassLoader());
this.key = in.readString();
this.value = in.readParcelable(Object.class.getClassLoader());
}
public static final Parcelable.Creator<MetaDatum> CREATOR = new Parcelable.Creator<MetaDatum>() {
@Override
public MetaDatum createFromParcel(Parcel source) {
return new MetaDatum(source);
}
@Override
public MetaDatum[] newArray(int size) {
return new MetaDatum[size];
}
};
}
答案 0 :(得分:0)
您的meta_data是数组而不是对象
meta_data": [
{
"id": 3281,
"key": "_vc_post_settings",
"value": {
"vc_grid_id": []
}
]
所以使用列表
private List<MetaDatum> metaData;
答案 1 :(得分:0)
使用robopojo puligns或http://www.jsonschema2pojo.org/
为您的json数据创建所有pojo类之后,使改造对象定义基本URL和其他内容...
public class ApiClient {
private final static String BASE_URL = "https://goorab.com/wp-json/wc/v2/";
public static ApiClient apiClient;
private Retrofit retrofit = null;
public static ApiClient getInstance() {
if (apiClient == null) {
apiClient = new ApiClient();
}
return apiClient;
}
//private static Retrofit storeRetrofit = null;
public Retrofit getClient() {
return getClient(null);
}
private Retrofit getClient(final Context context) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.readTimeout(60, TimeUnit.SECONDS);
client.writeTimeout(60, TimeUnit.SECONDS);
client.connectTimeout(60, TimeUnit.SECONDS);
client.addInterceptor(interceptor);
client.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
return chain.proceed(request);
}
});
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
}
并创建用于api调用的接口。
public interface ApiInterface {
@GET("products")
Call<ResponseData> getdata(@Query("consumer_key") String key);
}
之后调用如下活动或片段。
ApiInterface apiInterface = ApiClient.getInstance().getClient().create(ApiInterface.class);
Call<ResponseData> responseCall = apiInterface.getdata("pass key");
responseCall.enqueue(new Callback<ResponseData>() {
@Override
public void onResponse(Call<ResponseData> call, retrofit2.Response<ResponseData> response) {
if (response.isSuccessful() && response.body() != null && response != null) {
Toast.makeText(getApplicationContext(), "GetData" + response.body().getLanguage(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ResponseData> call, Throwable t) {
Log.d("Errror", t.getMessage());
}
});
并确保pojo类均有效。
答案 2 :(得分:0)
所有Unicode字符都可以放在引号内,但必须转义的字符除外:引号,反斜线和控制字符(U + 0000至U + 001F)