【问题标题】:Gson Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $Gson 预期 BEGIN_ARRAY 但在第 1 行第 2 列路径 $ 处为 BEGIN_OBJECT
【发布时间】:2017-06-05 14:35:51
【问题描述】:

这是我的数组列表

 ArrayList<Eat> eatList = gson.fromJson(jsonString, new 
 TypeToken<ArrayList<Eat>>() {
            }.getType());

这是我的 json:http://www.mocky.io/v2/592fdc32110000ef12b392cc

这是我的模型

public class Eat{

private String title,firstItemTitle,firstItemSutitle,
secondItemTitle,secondItemSutitle,
firstItemPrice,secondItemPrice,
firstItemImage,secondItemImage;


public String getTitle() {
    return title;
}

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

public String getFirstItemTitle() {
    return firstItemTitle;
}

public void setFirstItemTitle(String firstItemTitle) {
    this.firstItemTitle = firstItemTitle;
}

public String getFirstItemSutitle() {
    return firstItemSutitle;
}

 public void setFirstItemSutitle(String firstItemSutitle) {
    this.firstItemSutitle = firstItemSutitle;
 }

 }

【问题讨论】:

  • 请学习一些基础知识......你的 json 不是数组
  • 大声笑,再次学习基础知识并自己实现它或使用任何容易找到并将您的json转换为模型类的工具

标签: android json android-volley


【解决方案1】:

由于您的 JSON 不是 JSON 数组,而是包含数组的 JSON object,因此您需要编写一个包含 ArrayList 的类:

public class EatResponse {
    @SerializedName("eat")
    private ArrayList<Eat> eatList;

    public ArrayList<Eat> getEatList() {
        return eatList;
    }
}

然后,您只需要使用如下所示的调用从 JSON 中解析它:

EatResponse response = gson.fromJson(json, EatResponse.class);
ArrayList<Eat> eatList = response.getEatList();

【讨论】:

    猜你喜欢
    • 2020-01-06
    • 2017-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    相关资源
    最近更新 更多