gpt4 book ai didi

android - 如何使用 GSON 库解析这个 jsonarray

转载 作者:行者123 更新时间:2023-11-29 17:18:34 25 4
gpt4 key购买 nike

这是我的 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));

最佳答案

像这样创建一个模型类。

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 will give you all list of items.

    InputStream source = retrieveStream(url);
Gson gson = new Gson();
Reader reader = new InputStreamReader(source);
HomeCollections response = gson.fromJson(reader, HomeCollections.class);

关于android - 如何使用 GSON 库解析这个 jsonarray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37652945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com