这是我的jsonarray。
[
{
"id": 3,
"title": "Best Seller",
"promotedProducts": [
{
"product": {
"id": 4208,
"name": "Gents T-Shirt With Navy Blue Collar cuff",
"reviewList": [],
"productDetail": {
"id": 4207,
"length": 33,
"breadth": 27
},
"attributeList": [
{
"id": 1,
"productId": 4208
}
]
}
},
{
"product": {
"id": 4208,
"name": "Gents T-Shirt With Navy Blue Collar cuff",
"reviewList": [],
"productDetail": {
"id": 4207,
"length": 33,
"breadth": 27
},
"attributeList": [
{
"id": 1,
"productId": 4208
}
]
}
}
]
}
]
我为它创建了Homecollection类。还添加了以下代码进行解析。我还为product,promoteProducts,productDetail,attributeList,images.its创建了子类。我给出了两个项目的响应,但其他细节都是空的
Gson gson = new Gson();
HomeProducts homeProducts = HomeProducts.getInstance();
List<HomeCollections> collectionList = new ArrayList<HomeCollections>();
collectionList = Arrays.asList(gson.fromJson(response.toString(), HomeCollections[].class));
答案 0 :(得分:2)
像这样创建一个Model类。
public class ProductDetail
{
public int id { get; set; }
public int length { get; set; }
public int breadth { get; set; }
}
public class AttributeList
{
public int id { get; set; }
public int productId { get; set; }
}
public class Product
{
public int id { get; set; }
public string name { get; set; }
public List<object> reviewList { get; set; }
public ProductDetail productDetail { get; set; }
public List<AttributeList> attributeList { get; set; }
}
public class PromotedProduct
{
public Product product { get; set; }
}
public class HomeCollections
{
public int id { get; set; }
public string title { get; set; }
public List<PromotedProduct> promotedProducts { get; set; }
}
现在像这样使用GSON。这里的url是源链接。您将获得响应作为模型。现在
response.promotedProducts将为您提供所有项目清单。
InputStream source = retrieveStream(url);
Gson gson = new Gson();
Reader reader = new InputStreamReader(source);
HomeCollections response = gson.fromJson(reader, HomeCollections.class);
答案 1 :(得分:1)
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Generated("org.jsonschema2pojo")
public class AttributeList {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("productId")
@Expose
private Integer productId;
/**
*
* @return
* The id
*/
public Integer getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(Integer id) {
this.id = id;
}
/**
*
* @return
* The productId
*/
public Integer getProductId() {
return productId;
}
/**
*
* @param productId
* The productId
*/
public void setProductId(Integer productId) {
this.productId = productId;
}
}
-----------------------------------com.example.Example.java-----------------------------------
package com.example;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Generated("org.jsonschema2pojo")
public class Example {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("title")
@Expose
private String title;
@SerializedName("promotedProducts")
@Expose
private List<PromotedProduct> promotedProducts = new ArrayList<PromotedProduct>();
/**
*
* @return
* The id
*/
public Integer getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(Integer id) {
this.id = id;
}
/**
*
* @return
* The title
*/
public String getTitle() {
return title;
}
/**
*
* @param title
* The title
*/
public void setTitle(String title) {
this.title = title;
}
/**
*
* @return
* The promotedProducts
*/
public List<PromotedProduct> getPromotedProducts() {
return promotedProducts;
}
/**
*
* @param promotedProducts
* The promotedProducts
*/
public void setPromotedProducts(List<PromotedProduct> promotedProducts) {
this.promotedProducts = promotedProducts;
}
}
-----------------------------------com.example.Product.java-----------------------------------
package com.example;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
@Generated("org.jsonschema2pojo")
public class Product {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("reviewList")
@Expose
private List<Object> reviewList = new ArrayList<Object>();
private ProductDetail productDetail;
private List<AttributeList> attributeList = new ArrayList<AttributeList>();
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 List<Object> getReviewList() {
return reviewList;
}
public void setReviewList(List<Object> reviewList) {
this.reviewList = reviewList;
}
public ProductDetail getProductDetail() {
return productDetail;
}
public void setProductDetail(ProductDetail productDetail) {
this.productDetail = productDetail;
}
public List<AttributeList> getAttributeList() {
return attributeList;
}
public void setAttributeList(List<AttributeList> attributeList) {
this.attributeList = attributeList;
}
}
-----------------------------------com.example.ProductDetail.java-----------------------------------
package com.example;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class ProductDetail {
private Integer id;
private Integer length;
private Integer breadth;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getLength() {
return length;
}
public void setLength(Integer length) {
this.length = length;
}
public Integer getBreadth() {
return breadth;
}
public void setBreadth(Integer breadth) {
this.breadth = breadth;
}
}
-----------------------------------com.example.PromotedProduct.java-----------------------------------
package com.example;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class PromotedProduct {
private Product product;
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
}
使用此类来获取此数据
答案 2 :(得分:0)
您可以使用以下网站进行解析:
版本将是:
public class PromotedProduct {
@SerializedName("product")
@Expose
private Product product;
/**
*
* @return
* The product
*/
public Product getProduct() {
return product;
}
/**
*
* @param product
* The product
*/
public void setProduct(Product product) {
this.product = product;
}
}
ProductDetail:
public class ProductDetail {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("length")
@Expose
private Integer length;
@SerializedName("breadth")
@Expose
private Integer breadth;
/**
*
* @return
* The id
*/
public Integer getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(Integer id) {
this.id = id;
}
/**
*
* @return
* The length
*/
public Integer getLength() {
return length;
}
/**
*
* @param length
* The length
*/
public void setLength(Integer length) {
this.length = length;
}
/**
*
* @return
* The breadth
*/
public Integer getBreadth() {
return breadth;
}
/**
*
* @param breadth
* The breadth
*/
public void setBreadth(Integer breadth) {
this.breadth = breadth;
}
}
属性:
public class AttributeList {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("productId")
@Expose
private Integer productId;
/**
*
* @return
* The id
*/
public Integer getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(Integer id) {
this.id = id;
}
/**
*
* @return
* The productId
*/
public Integer getProductId() {
return productId;
}
/**
*
* @param productId
* The productId
*/
public void setProductId(Integer productId) {
this.productId = productId;
}
}
主要课程:
public class Example {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("title")
@Expose
private String title;
@SerializedName("promotedProducts")
@Expose
private List<PromotedProduct> promotedProducts = new ArrayList<PromotedProduct>();
/**
*
* @return
* The id
*/
public Integer getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(Integer id) {
this.id = id;
}
/**
*
* @return
* The title
*/
public String getTitle() {
return title;
}
/**
*
* @param title
* The title
*/
public void setTitle(String title) {
this.title = title;
}
/**
*
* @return
* The promotedProducts
*/
public List<PromotedProduct> getPromotedProducts() {
return promotedProducts;
}
/**
*
* @param promotedProducts
* The promotedProducts
*/
public void setPromotedProducts(List<PromotedProduct> promotedProducts) {
this.promotedProducts = promotedProducts;
}
}
最后一个产品:
public class Product {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("reviewList")
@Expose
private List<Object> reviewList = new ArrayList<Object>();
@SerializedName("productDetail")
@Expose
private ProductDetail productDetail;
@SerializedName("attributeList")
@Expose
private List<AttributeList> attributeList = new ArrayList<AttributeList>();
/**
*
* @return
* The id
*/
public Integer getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(Integer id) {
this.id = id;
}
/**
*
* @return
* The name
*/
public String getName() {
return name;
}
/**
*
* @param name
* The name
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @return
* The reviewList
*/
public List<Object> getReviewList() {
return reviewList;
}
/**
*
* @param reviewList
* The reviewList
*/
public void setReviewList(List<Object> reviewList) {
this.reviewList = reviewList;
}
/**
*
* @return
* The productDetail
*/
public ProductDetail getProductDetail() {
return productDetail;
}
/**
*
* @param productDetail
* The productDetail
*/
public void setProductDetail(ProductDetail productDetail) {
this.productDetail = productDetail;
}
/**
*
* @return
* The attributeList
*/
public List<AttributeList> getAttributeList() {
return attributeList;
}
/**
*
* @param attributeList
* The attributeList
*/
public void setAttributeList(List<AttributeList> attributeList) {
this.attributeList = attributeList;
}
}
当然你需要稍微改进一下。我只是复制粘贴生成的文件。您可以在#34;示例&#34;中更改您的主要课程。无论你喜欢什么,再加上一些你不喜欢的东西。