Retrofit2:预期为BEGIN_ARRAY,但在第1行第268列的路径$ [0]处为STRING. [英] Retrofit2:Expected BEGIN_ARRAY but was STRING at line 1 column 268 path $[0].images

查看:112
本文介绍了Retrofit2:预期为BEGIN_ARRAY,但在第1行第268列的路径$ [0]处为STRING.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这不是第一次有人问这个问题,但是有了Retrofit2,我找不到解决我问题的正确方法.

I know this is not the first time someone asking about this problem but with Retrofit2 I can't find the right solution to my problem.

我有一个包含字符串列表的对象.当我想将JSON响应转换为我的对象时,所有其他字段都可以,但是将字符串列表转换为列表时出现此错误:

I have an object that contains a list of String. when I want to convert JSON response to my object all other fields are ok but I got this error for converting the list of string to my list:

Retrofit2: Expected BEGIN_ARRAY but was STRING at line 1 column 268 path $[0].images

这是我的API:

@POST("/cp/api/")// get list of products
    Call<List<Product>> Get_Special_Products(@Body Object request);

我的改造设置:

public Retrofit Store_retrofit(OkHttpClient client) {
        return new Retrofit.Builder()
                .baseUrl(Urls.Sotre_Base_Url)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

我的对象:

public class Product implements Serializable {
    @SerializedName("id")
    private int id;
    @SerializedName("user_id")
    private int user_id;
    @SerializedName("cat_id")
    private int cat_id;
    @SerializedName("title")
    private String title;
    @SerializedName("description")
    private String description;
    @SerializedName("image")
    private String image;
    @SerializedName("images")
    private List<String> images;
public int getUser_id() {
        return user_id;
    }

    public int getCat_id() {
        return cat_id;
    }

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }

    public String getImage() {
        return image;
    }
public List<String> getImages() {
        return images;
    }
}

这是JSON的一部分,导致图像错误:

and this a part of JSON that cause the error for image:

images:[
    "1487801544.jpg","1487801544.jpg","1487801544.jpg"
]

推荐答案

这种情况通常发生在您的API服务无法将数组转换为json且改型将其读取为String的情况下. 调用您的API服务提供商以解决将数组转换为json :) 例如

This mostly happens when your API service can not convert array to json and retrofit reads it as String . call to your API service provider to solve converting array to json :) for example

"images": "[\"1487801544.jpg\",\"1487801544.jpg\",\"148801544.jpg\"]"

上面的改型读为String,应进行如下更改:

retrofit read above as String and should be change as below :

"images": [
      "1487801544.jpg",
      "1487801544.jpg",
      "1487801544.jpg"
    ]

这篇关于Retrofit2:预期为BEGIN_ARRAY,但在第1行第268列的路径$ [0]处为STRING.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆