【问题标题】:com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 2 column 1 pathcom.google.gson.JsonSyntaxException: java.lang.IllegalStateException: 预期 BEGIN_ARRAY 但在第 2 行第 1 列路径为 STRING
【发布时间】:2016-06-07 07:54:49
【问题描述】:

我的回应是,

[
{
"category_name":"Vaporizers",
"categoryid":"3",
"status":"1",
"description":"<div class= bennar_out_div >\r\n<div class= container >\r\n<div class= viporizers_text >\r\n<div class= viporizers_text_left >\r\n<div class= viporizers_head >Vaporizers</div>\r\n<div class= viporizers_text1 >Whether you&rsquo;re looking for a portable or a desktop, GroovyVapes offers a wide variety of the highest quality vaporizers available. Every vaporizer that is part of the GroovyVapes lineup is thoroughly evaluated and tested and reviewed by our team to ensure its quality, design, and user-satisfaction.</div>\r\n</div>\r\n<div class= viporizers_text_right >FREE SHIPPING <span>over&nbsp;</span>&pound;85.00<br />NEWEST MODELS<br />PRIVATE &amp; SECURE</div>\r\n</div>\r\n</div>\r\n</div>\r\n<div style= clear: both; >&nbsp;</div>",
"image":false,
"count":0,
"subcategory_name":[
{
"sub_category_name":"Portable",
"sub_categoryid":"10",
"status":"1",
"description":"<div class= bennar_out_div >\r\n<div class= container >\r\n<div class= viporizers_text >\r\n<div class= viporizers_text_left >\r\n<div class= viporizers_head >PORTABLE</div>\r\n<div class= viporizers_text1 >Portable Vaporizers are best used for vaping on the go. Most portable vaporizers are battery powered, with many being easily chargeable via USB.</div>\r\n</div>\r\n<div class= viporizers_text_right >FREE SHIPPING <span>over&nbsp;</span>&pound;85.00<br />NEWEST MODELS<br />PRIVATE &amp; SECURE</div>\r\n</div>\r\n</div>\r\n</div>\r\n<div style= clear: both; >&nbsp;</div>",
"image":"http://www.groovyvapes.co.uk/media/catalog/category/portable.png",
"count":26
}}
]

我的代码:

ArrayList<BrandAccessoriesResponseModel> mBrandAccessoriesResponseModelArrayList = new ArrayList<BrandAccessoriesResponseModel>();

Gson gson = new Gson();
            Type listType = new TypeToken<List<BrandAccessoriesResponseModel>>() {}.getType();
            mBrandAccessoriesResponseModelArrayList = gson.fromJson(result.toString(), listType);

【问题讨论】:

标签: android gson


【解决方案1】:

请检查您的代码,并再次调用API,您的代码是正确的,不需要更改,,。再次检查您的调用 API。

【讨论】:

    【解决方案2】:

    一个有效的 json 应该是这样的:

        [
         {
          "category_name":"Vaporizers",
          "categoryid":"3",
          "status":"1",
          "description":"<div class= bennar_out_div >\r\n<div class= container >\r\n<div class= viporizers_text >\r\n<div class= viporizers_text_left >\r\n<div class= viporizers_head >Vaporizers</div>\r\n<div class= viporizers_text1 >Whether you&rsquo;re looking for a portable or a desktop, GroovyVapes offers a wide variety of the highest quality vaporizers available. Every vaporizer that is part of the GroovyVapes lineup is thoroughly evaluated and tested and reviewed by our team to ensure its quality, design, and user-satisfaction.</div>\r\n</div>\r\n<div class= viporizers_text_right >FREE SHIPPING <span>over&nbsp;</span>&pound;85.00<br />NEWEST MODELS<br />PRIVATE &amp; SECURE</div>\r\n</div>\r\n</div>\r\n</div>\r\n<div style= clear: both; >&nbsp;</div>",
          "image":false,
          "count":0,
          "subcategory_name":
          [
              {
                  "sub_category_name":"Portable",
                  "sub_categoryid":"10",
                  "status":"1",
                  "description":"<div class= bennar_out_div >\r\n<div class= container >\r\n<div class= viporizers_text >\r\n<div class= viporizers_text_left >\r\n<div class= viporizers_head >PORTABLE</div>\r\n<div class= viporizers_text1 >Portable Vaporizers are best used for vaping on the go. Most portable vaporizers are battery powered, with many being easily chargeable via USB.</div>\r\n</div>\r\n<div class= viporizers_text_right >FREE SHIPPING <span>over&nbsp;</span>&pound;85.00<br />NEWEST MODELS<br />PRIVATE &amp; SECURE</div>\r\n</div>\r\n</div>\r\n</div>\r\n<div style= clear: both; >&nbsp;</div>",
                  "image":"http://www.groovyvapes.co.uk/media/catalog/category/portable.png",
                  "count":26
            }
          ]
        }
      ]
    

    现在,使用 jsonschema2pojo:
    jsonschema2pojo

    您可以拥有以下内容:

    public class Example {
    
    @SerializedName("category_name")
    @Expose
    private String categoryName;
    @SerializedName("categoryid")
    @Expose
    private String categoryid;
    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("description")
    @Expose
    private String description;
    @SerializedName("image")
    @Expose
    private Boolean image;
    @SerializedName("count")
    @Expose
    private Integer count;
    @SerializedName("subcategory_name")
    @Expose
    private List<SubcategoryName> subcategoryName = new ArrayList<SubcategoryName>();
    
    /**
    * 
    * @return
    * The categoryName
    */
    public String getCategoryName() {
    return categoryName;
    }
    
    /**
    * 
    * @param categoryName
    * The category_name
    */
    public void setCategoryName(String categoryName) {
    this.categoryName = categoryName;
    }
    
    /**
    * 
    * @return
    * The categoryid
    */
    public String getCategoryid() {
    return categoryid;
    }
    
    /**
    * 
    * @param categoryid
    * The categoryid
    */
    public void setCategoryid(String categoryid) {
    this.categoryid = categoryid;
    }
    
    /**
    * 
    * @return
    * The status
    */
    public String getStatus() {
    return status;
    }
    
    /**
    * 
    * @param status
    * The status
    */
    public void setStatus(String status) {
    this.status = status;
    }
    
    /**
    * 
    * @return
    * The description
    */
    public String getDescription() {
    return description;
    }
    
    /**
    * 
    * @param description
    * The description
    */
    public void setDescription(String description) {
    this.description = description;
    }
    
    /**
    * 
    * @return
    * The image
    */
    public Boolean getImage() {
    return image;
    }
    
    /**
    * 
    * @param image
    * The image
    */
    public void setImage(Boolean image) {
    this.image = image;
    }
    
    /**
    * 
    * @return
    * The count
    */
    public Integer getCount() {
    return count;
    }
    
    /**
    * 
    * @param count
    * The count
    */
    public void setCount(Integer count) {
    this.count = count;
    }
    
    /**
    * 
    * @return
    * The subcategoryName
    */
    public List<SubcategoryName> getSubcategoryName() {
    return subcategoryName;
    }
    
    /**
    * 
    * @param subcategoryName
    * The subcategory_name
    */
    public void setSubcategoryName(List<SubcategoryName> subcategoryName) {
    this.subcategoryName = subcategoryName;
    }
    
    }
    -----------------------------------com.example.SubcategoryName.java-----------------------------------
    
    package com.example;
    
    import javax.annotation.Generated;
    import com.google.gson.annotations.Expose;
    import com.google.gson.annotations.SerializedName;
    
    @Generated("org.jsonschema2pojo")
    public class SubcategoryName {
    
    @SerializedName("sub_category_name")
    @Expose
    private String subCategoryName;
    @SerializedName("sub_categoryid")
    @Expose
    private String subCategoryid;
    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("description")
    @Expose
    private String description;
    @SerializedName("image")
    @Expose
    private String image;
    @SerializedName("count")
    @Expose
    private Integer count;
    
    /**
    * 
    * @return
    * The subCategoryName
    */
    public String getSubCategoryName() {
    return subCategoryName;
    }
    
    /**
    * 
    * @param subCategoryName
    * The sub_category_name
    */
    public void setSubCategoryName(String subCategoryName) {
    this.subCategoryName = subCategoryName;
    }
    
    /**
    * 
    * @return
    * The subCategoryid
    */
    public String getSubCategoryid() {
    return subCategoryid;
    }
    
    /**
    * 
    * @param subCategoryid
    * The sub_categoryid
    */
    public void setSubCategoryid(String subCategoryid) {
    this.subCategoryid = subCategoryid;
    }
    
    /**
    * 
    * @return
    * The status
    */
    public String getStatus() {
    return status;
    }
    
    /**
    * 
    * @param status
    * The status
    */
    public void setStatus(String status) {
    this.status = status;
    }
    
    /**
    * 
    * @return
    * The description
    */
    public String getDescription() {
    return description;
    }
    
    /**
    * 
    * @param description
    * The description
    */
    public void setDescription(String description) {
    this.description = description;
    }
    
    /**
    * 
    * @return
    * The image
    */
    public String getImage() {
    return image;
    }
    
    /**
    * 
    * @param image
    * The image
    */
    public void setImage(String image) {
    this.image = image;
    }
    
    /**
    * 
    * @return
    * The count
    */
    public Integer getCount() {
    return count;
    }
    
    /**
    * 
    * @param count
    * The count
    */
    public void setCount(Integer count) {
    this.count = count;
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-29
      • 1970-01-01
      • 1970-01-01
      • 2020-03-10
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      • 1970-01-01
      相关资源
      最近更新 更多