0
我知道這個問題已經提出了很多,但到目前爲止,我沒有真正找到答案...Android - 如何訪問嵌套的對象?
我想與GSON一起使用Android。我想使用JSON字符串填充Gridview,但我不知道如何訪問嵌套的對象。
JSON文件:
[{'ProductCategories':[
{
'name':'Cat1', 'Product Series':[
{
'name':'ProdSeries1', 'Description':'Lorem Ipsum Bla Bla','Products':[
{
'name':'Product1','key':'value','key':'...'
},
{
'name':'Product2','key':'value','key':'...'
},
]
}
]
},
]
}]
我做了4類:Products
,ProductSeries
,ProductCatalog
和ProductCategory
。
例如:
public class ProductCatalog {
@SerializedName("ProductCategories")
@Expose
private List<ProductCategory> productCategories = null;
public List<ProductCategory> getProductCategories() {
return productCategories;
}
public void setProductCategories(List<ProductCategory> productCategories) {
this.productCategories = productCategories;
}
}
之後,我分析的JSON與GSON:
Gson gson = new Gson();
Type type = new TypeToken<List<ProductCatalog>>(){}.getType();
List<ProductCatalog> productcatalog = gson.fromJson(JSONstring,type);
現在我有JSON數據的分析列表,但不知道如何工作的與'Product1'嵌套的對象。我認爲獲得者會有所幫助,但我無法在我的活動中訪問getProductCategories()
。我怎樣才能做到這一點?
感謝這個答案。但是我的類看起來完全一樣......在'List productcatalog = gson.fromJson(JSONstring,type)之後;'我不能使用getter'productcatalog.getProductCategories()' –
Mike
您已經替換了主類('MainClazz ''用'ProductCatalog'命名,仍然沒有得到 –
發佈完整的json –