如何使用GSON解析以下JSON对象:
{
"ProductsByCategory": [
{.....},
{.....},
{.....},
{.....},
]
}
JSON对象包含一系列元素。我正在使用GSON来尝试解析JSON并使用以下POJO类来帮助我。
public class ProductItems {
@SerializedName("ProductsByCategory")
@Expose
private List<ProductsByCategory> productsByCategory = null;
public List<ProductsByCategory> getProductsByCategory() {
return productsByCategory;
}
public void setProductsByCategory(List<ProductsByCategory> productsByCategory) {
this.productsByCategory = productsByCategory;
}
}
public class ProductsByCategory {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("slug")
@Expose
private String slug;
@SerializedName("permalink")
@Expose
private String permalink;
@SerializedName("date_created")
@Expose
private String dateCreated;
@SerializedName("date_created_gmt")
@Expose
private String dateCreatedGmt;
@SerializedName("date_modified")
@Expose
private String dateModified;
@SerializedName("date_modified_gmt")
@Expose
private String dateModifiedGmt;
@SerializedName("type")
@Expose
private String type;
@SerializedName("status")
@Expose
private String status;
@SerializedName("featured")
@Expose
private Boolean featured;
@SerializedName("catalog_visibility")
@Expose
private String catalogVisibility;
@SerializedName("description")
@Expose
private String description;
@SerializedName("short_description")
@Expose
private String shortDescription;
@SerializedName("sku")
@Expose
private String sku;
@SerializedName("price")
@Expose
private String price;
@SerializedName("regular_price")
@Expose
private String regularPrice;
@SerializedName("sale_price")
@Expose
private String salePrice;
@SerializedName("date_on_sale_from")
@Expose
private Object dateOnSaleFrom;
@SerializedName("date_on_sale_from_gmt")
@Expose
private Object dateOnSaleFromGmt;
@SerializedName("date_on_sale_to")
@Expose
private Object dateOnSaleTo;
@SerializedName("date_on_sale_to_gmt")
@Expose
private Object dateOnSaleToGmt;
@SerializedName("price_html")
@Expose
private String priceHtml;
@SerializedName("on_sale")
@Expose
private Boolean onSale;
@SerializedName("purchasable")
@Expose
private Boolean purchasable;
@SerializedName("total_sales")
@Expose
private Integer totalSales;
@SerializedName("virtual")
@Expose
private Boolean virtual;
@SerializedName("downloadable")
@Expose
private Boolean downloadable;
@SerializedName("downloads")
@Expose
private List<Object> downloads = null;
@SerializedName("download_limit")
@Expose
private Integer downloadLimit;
@SerializedName("download_expiry")
@Expose
private Integer downloadExpiry;
@SerializedName("external_url")
@Expose
private String externalUrl;
@SerializedName("button_text")
@Expose
private String buttonText;
@SerializedName("tax_status")
@Expose
private String taxStatus;
@SerializedName("tax_class")
@Expose
private String taxClass;
@SerializedName("manage_stock")
@Expose
private Boolean manageStock;
@SerializedName("stock_quantity")
@Expose
private Integer stockQuantity;
@SerializedName("in_stock")
@Expose
private Boolean inStock;
@SerializedName("backorders")
@Expose
private String backorders;
@SerializedName("backorders_allowed")
@Expose
private Boolean backordersAllowed;
@SerializedName("backordered")
@Expose
private Boolean backordered;
@SerializedName("sold_individually")
@Expose
private Boolean soldIndividually;
@SerializedName("weight")
@Expose
private String weight;
@SerializedName("dimensions")
@Expose
private Dimensions dimensions;
@SerializedName("shipping_required")
@Expose
private Boolean shippingRequired;
@SerializedName("shipping_taxable")
@Expose
private Boolean shippingTaxable;
@SerializedName("shipping_class")
@Expose
private String shippingClass;
@SerializedName("shipping_class_id")
@Expose
private Integer shippingClassId;
@SerializedName("reviews_allowed")
@Expose
private Boolean reviewsAllowed;
@SerializedName("average_rating")
@Expose
private String averageRating;
@SerializedName("rating_count")
@Expose
private Integer ratingCount;
@SerializedName("related_ids")
@Expose
private List<Integer> relatedIds = null;
@SerializedName("upsell_ids")
@Expose
private List<Object> upsellIds = null;
@SerializedName("cross_sell_ids")
@Expose
private List<Object> crossSellIds = null;
@SerializedName("parent_id")
@Expose
private Integer parentId;
@SerializedName("purchase_note")
@Expose
private String purchaseNote;
@SerializedName("categories")
@Expose
private List<Category> categories = null;
@SerializedName("tags")
@Expose
private List<Object> tags = null;
@SerializedName("images")
@Expose
private List<Image> images = null;
@SerializedName("attributes")
@Expose
private List<Attribute> attributes = null;
@SerializedName("default_attributes")
@Expose
private List<Object> defaultAttributes = null;
@SerializedName("variations")
@Expose
private List<Integer> variations = null;
@SerializedName("grouped_products")
@Expose
private List<Object> groupedProducts = null;
@SerializedName("menu_order")
@Expose
private Integer menuOrder;
@SerializedName("meta_data")
@Expose
private List<MetaDatum> metaData = null;
@SerializedName("_links")
@Expose
private Links links;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public String getPermalink() {
return permalink;
}
public void setPermalink(String permalink) {
this.permalink = permalink;
}
public String getDateCreated() {
return dateCreated;
}
public void setDateCreated(String dateCreated) {
this.dateCreated = dateCreated;
}
public String getDateCreatedGmt() {
return dateCreatedGmt;
}
public void setDateCreatedGmt(String dateCreatedGmt) {
this.dateCreatedGmt = dateCreatedGmt;
}
public String getDateModified() {
return dateModified;
}
public void setDateModified(String dateModified) {
this.dateModified = dateModified;
}
public String getDateModifiedGmt() {
return dateModifiedGmt;
}
public void setDateModifiedGmt(String dateModifiedGmt) {
this.dateModifiedGmt = dateModifiedGmt;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Boolean getFeatured() {
return featured;
}
public void setFeatured(Boolean featured) {
this.featured = featured;
}
public String getCatalogVisibility() {
return catalogVisibility;
}
public void setCatalogVisibility(String catalogVisibility) {
this.catalogVisibility = catalogVisibility;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getShortDescription() {
return shortDescription;
}
public void setShortDescription(String shortDescription) {
this.shortDescription = shortDescription;
}
public String getSku() {
return sku;
}
public void setSku(String sku) {
this.sku = sku;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getRegularPrice() {
return regularPrice;
}
public void setRegularPrice(String regularPrice) {
this.regularPrice = regularPrice;
}
public String getSalePrice() {
return salePrice;
}
public void setSalePrice(String salePrice) {
this.salePrice = salePrice;
}
public Object getDateOnSaleFrom() {
return dateOnSaleFrom;
}
public void setDateOnSaleFrom(Object dateOnSaleFrom) {
this.dateOnSaleFrom = dateOnSaleFrom;
}
public Object getDateOnSaleFromGmt() {
return dateOnSaleFromGmt;
}
public void setDateOnSaleFromGmt(Object dateOnSaleFromGmt) {
this.dateOnSaleFromGmt = dateOnSaleFromGmt;
}
public Object getDateOnSaleTo() {
return dateOnSaleTo;
}
public void setDateOnSaleTo(Object dateOnSaleTo) {
this.dateOnSaleTo = dateOnSaleTo;
}
public Object getDateOnSaleToGmt() {
return dateOnSaleToGmt;
}
public void setDateOnSaleToGmt(Object dateOnSaleToGmt) {
this.dateOnSaleToGmt = dateOnSaleToGmt;
}
public String getPriceHtml() {
return priceHtml;
}
public void setPriceHtml(String priceHtml) {
this.priceHtml = priceHtml;
}
public Boolean getOnSale() {
return onSale;
}
public void setOnSale(Boolean onSale) {
this.onSale = onSale;
}
public Boolean getPurchasable() {
return purchasable;
}
public void setPurchasable(Boolean purchasable) {
this.purchasable = purchasable;
}
public Integer getTotalSales() {
return totalSales;
}
public void setTotalSales(Integer totalSales) {
this.totalSales = totalSales;
}
public Boolean getVirtual() {
return virtual;
}
public void setVirtual(Boolean virtual) {
this.virtual = virtual;
}
public Boolean getDownloadable() {
return downloadable;
}
public void setDownloadable(Boolean downloadable) {
this.downloadable = downloadable;
}
public List<Object> getDownloads() {
return downloads;
}
public void setDownloads(List<Object> downloads) {
this.downloads = downloads;
}
public Integer getDownloadLimit() {
return downloadLimit;
}
public void setDownloadLimit(Integer downloadLimit) {
this.downloadLimit = downloadLimit;
}
public Integer getDownloadExpiry() {
return downloadExpiry;
}
public void setDownloadExpiry(Integer downloadExpiry) {
this.downloadExpiry = downloadExpiry;
}
public String getExternalUrl() {
return externalUrl;
}
public void setExternalUrl(String externalUrl) {
this.externalUrl = externalUrl;
}
public String getButtonText() {
return buttonText;
}
public void setButtonText(String buttonText) {
this.buttonText = buttonText;
}
public String getTaxStatus() {
return taxStatus;
}
public void setTaxStatus(String taxStatus) {
this.taxStatus = taxStatus;
}
public String getTaxClass() {
return taxClass;
}
public void setTaxClass(String taxClass) {
this.taxClass = taxClass;
}
public Boolean getManageStock() {
return manageStock;
}
public void setManageStock(Boolean manageStock) {
this.manageStock = manageStock;
}
public Integer getStockQuantity() {
return stockQuantity;
}
public void setStockQuantity(Integer stockQuantity) {
this.stockQuantity = stockQuantity;
}
public Boolean getInStock() {
return inStock;
}
public void setInStock(Boolean inStock) {
this.inStock = inStock;
}
public String getBackorders() {
return backorders;
}
public void setBackorders(String backorders) {
this.backorders = backorders;
}
public Boolean getBackordersAllowed() {
return backordersAllowed;
}
public void setBackordersAllowed(Boolean backordersAllowed) {
this.backordersAllowed = backordersAllowed;
}
public Boolean getBackordered() {
return backordered;
}
public void setBackordered(Boolean backordered) {
this.backordered = backordered;
}
public Boolean getSoldIndividually() {
return soldIndividually;
}
public void setSoldIndividually(Boolean soldIndividually) {
this.soldIndividually = soldIndividually;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public Dimensions getDimensions() {
return dimensions;
}
public void setDimensions(Dimensions dimensions) {
this.dimensions = dimensions;
}
public Boolean getShippingRequired() {
return shippingRequired;
}
public void setShippingRequired(Boolean shippingRequired) {
this.shippingRequired = shippingRequired;
}
public Boolean getShippingTaxable() {
return shippingTaxable;
}
public void setShippingTaxable(Boolean shippingTaxable) {
this.shippingTaxable = shippingTaxable;
}
public String getShippingClass() {
return shippingClass;
}
public void setShippingClass(String shippingClass) {
this.shippingClass = shippingClass;
}
public Integer getShippingClassId() {
return shippingClassId;
}
public void setShippingClassId(Integer shippingClassId) {
this.shippingClassId = shippingClassId;
}
public Boolean getReviewsAllowed() {
return reviewsAllowed;
}
public void setReviewsAllowed(Boolean reviewsAllowed) {
this.reviewsAllowed = reviewsAllowed;
}
public String getAverageRating() {
return averageRating;
}
public void setAverageRating(String averageRating) {
this.averageRating = averageRating;
}
public Integer getRatingCount() {
return ratingCount;
}
public void setRatingCount(Integer ratingCount) {
this.ratingCount = ratingCount;
}
public List<Integer> getRelatedIds() {
return relatedIds;
}
public void setRelatedIds(List<Integer> relatedIds) {
this.relatedIds = relatedIds;
}
public List<Object> getUpsellIds() {
return upsellIds;
}
public void setUpsellIds(List<Object> upsellIds) {
this.upsellIds = upsellIds;
}
public List<Object> getCrossSellIds() {
return crossSellIds;
}
public void setCrossSellIds(List<Object> crossSellIds) {
this.crossSellIds = crossSellIds;
}
public Integer getParentId() {
return parentId;
}
public void setParentId(Integer parentId) {
this.parentId = parentId;
}
public String getPurchaseNote() {
return purchaseNote;
}
public void setPurchaseNote(String purchaseNote) {
this.purchaseNote = purchaseNote;
}
public List<Category> getCategories() {
return categories;
}
public void setCategories(List<Category> categories) {
this.categories = categories;
}
public List<Object> getTags() {
return tags;
}
public void setTags(List<Object> tags) {
this.tags = tags;
}
public List<Image> getImages() {
return images;
}
public void setImages(List<Image> images) {
this.images = images;
}
public List<Attribute> getAttributes() {
return attributes;
}
public void setAttributes(List<Attribute> attributes) {
this.attributes = attributes;
}
public List<Object> getDefaultAttributes() {
return defaultAttributes;
}
public void setDefaultAttributes(List<Object> defaultAttributes) {
this.defaultAttributes = defaultAttributes;
}
public List<Integer> getVariations() {
return variations;
}
public void setVariations(List<Integer> variations) {
this.variations = variations;
}
public List<Object> getGroupedProducts() {
return groupedProducts;
}
public void setGroupedProducts(List<Object> groupedProducts) {
this.groupedProducts = groupedProducts;
}
public Integer getMenuOrder() {
return menuOrder;
}
public void setMenuOrder(Integer menuOrder) {
this.menuOrder = menuOrder;
}
public List<MetaDatum> getMetaData() {
return metaData;
}
public void setMetaData(List<MetaDatum> metaData) {
this.metaData = metaData;
}
public Links getLinks() {
return links;
}
public void setLinks(Links links) {
this.links = links;
}
}
我使用的代码如下:
public void onResponse(Call call, Response response) throws IOException
{
String mMessage = response.body().string();
if (response.isSuccessful())
{
try
{
Gson gson = new Gson();
final ProductItems categoryProducts = gson.fromJson(mMessage, ProductItems.class);
response.close();
}
catch (Exception e)
{
Log.e("Error", "Failed to upload");
e.printStackTrace();
}
但是,我收到以下错误:GSON抛出“预期的BEGIN_OBJECT但是BEGIN_ARRAY。 我试图将ProductItems类更改为为productbycategories获取一个对象,但后来我收到以下错误:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:期望一个字符串,但在第1行第2324行路径$是BEGIN_OBJECT。 ProductsByCategory [0] .meta_data [0]。价值。我现在有点困惑。
答案 0 :(得分:1)
您收到此错误的原因是数据与POJO的生成方式不一致。
这是存在问题的数据的具体部分:
&#34; meta_data&#34;:[ { &#34; id&#34;:14232, &#34; key&#34;:&#34; _vc_post_settings&#34;, &#34; value&#34;:{ &#34; vc_grid_id&#34;:[] } 强> }, { &#34; id&#34;:14273, &#34; key&#34;:&#34; fb_product_description&#34;, &#34;价值&#34;:&#34;&#34; }, { &#34; id&#34;:14274, &#34; key&#34;:&#34; fb_visibility&#34;, &#34;价值&#34;:&#34; 1&#34; },
注意&#34; meta_data&#34; field是一个对象数组,这些对象包含一个&#34;值&#34;领域。 &#34;值的数据类型&#34;变化 - 有时它是一个对象,有时它是一个字符串。
一种可能的解决方案是更改MetaDatum
课程,以便value
为Object
:
package com.example;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class MetaDatum {
...
@SerializedName("value")
@Expose
private Object value;
...
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
}