gpt4 book ai didi

android - GSON 返回异常 json 语法异常需要一个字符串,但在第 1 行第 15 列是 begin_object

转载 作者:搜寻专家 更新时间:2023-11-01 09:03:15 28 4
gpt4 key购买 nike

下面是我的代码。当我运行时它显示“json 语法异常需要一个字符串但是是 begin_object”。我不知道为什么会显示错误。

 {
"products": [
{
"name": "gam",
"pplsft": "75665",
"imei": "Ptwm ",
"created_at": "2012-12-03 04:58:01"
},
{
"name": "",
"pplsft": "0",
"imei": "",
"created_at": "2012-12-03 05:44:01"
},
{
"name": "gptw",
"pplsft": "0",
"imei": "at",
"created_at": "2012-12-03 05:58:18"
},
{
"name": "",
"pplsft": "0",
"imei": "",
"created_at": "2012-12-03 23:32:06"
},
{
"name": "",
"pplsft": "0",
"imei": "",
"created_at": "2012-12-03 23:35:25"
}
]
}

和类文件是,但我不知道如何使用 gson 创建用于 json 解析的类文件。谁能解释一下??

  public class Results  {
public String name;
@SerializedName("pplsft")
public int pplsft;
@SerializedName("imei")
public String imei;
@SerializedName("created_at")
public int created_at;
}

public class SearchResponse {

@SerializedName("products")
public List<Result> products;
@SerializedName("name")
public String name;
@SerializedName("pplsft")
public int pplsft;
@SerializedName("imei")
public String imei;
@SerializedName("created_at")
public int created_at;
public List<Result> getProducts() {
return products;
}
public void setProducts(List<Result> products) {
this.products = products;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPplsft() {
return pplsft;
}
public void setPplsft(int pplsft) {
this.pplsft = pplsft;
}
public String getImei() {
return imei;
}
public void setImei(String imei) {
this.imei = imei;
}
public int getCreated_at() {
return created_at;
}
public void setCreated_at(int created_at) {
this.created_at = created_at;
}

}

这是从json中调用数据的主要方法。

    response = gson.fromJson(reader, SearchResponse.class);
Toast.makeText(this,response.name, Toast.LENGTH_SHORT).show();
List<Result> list = response.products;

最佳答案

对于 JSON,您的对象应该如下所示:

SearchResponse response = gson.fromJson(reader, SearchResponse.class);

然后获取您的列表:

List<Product> mProducts = response.products;

要遍历列表,您需要执行以下操作:

for( Product pro : mProducts ){
String pName = pro.name;
......
}

或者您可以手动完成(从第一个对象获取名称;

mProducts.get(0).name;

现在你的类(class):

public class SearchResponse  {

@SerializedName("products")
public List<Product> products;

public class Product {

@SerializedName("name")
public String name;

@SerializedName("pplsft")
public String pplsft;

@SerializedName("imei")
public String imei;

@SerializedName("created_at")
public String created_at;

}
}

你的 JSON

{
"products":[
{
"name":"gam",
"pplsft":"75665",
"imei":"Ptwm ",
"created_at":"2012-12-03 04:58:01"
},
{
"name":"",
"pplsft":"0",
"imei":"",
"created_at":"2012-12-03 05:44:01"
},
{
"name":"gptw",
"pplsft":"0",
"imei":"at",
"created_at":"2012-12-03 05:58:18"
},
{
"name":"",
"pplsft":"0",
"imei":"",
"created_at":"2012-12-03 23:32:06"
},
{
"name":"",
"pplsft":"0",
"imei":"",
"created_at":"2012-12-03 23:35:25"
}
]
}

Perhaps the Solution to this Post也可能对您有所帮助。

关于android - GSON 返回异常 json 语法异常需要一个字符串,但在第 1 行第 15 列是 begin_object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13696436/

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