gpt4 book ai didi

android - Gson 预期为 BEGIN_ARRAY 但在第 1 行第 2 列路径为 BEGIN_OBJECT $

转载 作者:行者123 更新时间:2023-11-29 15:39:07 26 4
gpt4 key购买 nike

这是我的数组列表

 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 数组,而是包含数组的 JSON 对象,因此您需要编写一个包含 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();

关于android - Gson 预期为 BEGIN_ARRAY 但在第 1 行第 2 列路径为 BEGIN_OBJECT $,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44371388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com