当前位置: 首页 > 知识库问答 >
问题:

GSON返回异常json语法异常应为字符串,但在第1行第15列为begin\u对象

易研
2023-03-14

下面是我的代码。当我运行时,它显示“json语法异常应为字符串,但为begin\u对象”。我不知道为什么它会显示错误。

 {
"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;

共有1个答案

闻人凯泽
2023-03-14

对于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;

现在您的班级:

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"
      }
   ]
}

也许这篇文章的解决方案也会对你有所帮助。

 类似资料: