这是我的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,promotedProducts,productDetail,attributeList,images的子类。它给了我作为对两项的响应,但其他细节为空
Gson gson = new Gson();
HomeProducts homeProducts = HomeProducts.getInstance();
List<HomeCollections> collectionList = new ArrayList<HomeCollections>();
collectionList = Arrays.asList(gson.fromJson(response.toString(), HomeCollections[].class));
最佳答案
创建一个这样的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);