使用Android改造解析动态密钥JSON数组



如何使用Android改造来使用此JSON?

{
"Pepsico": {
"items": [
{
"pro_name": "Lays indian mGic masala",
"pro_price": "0"
},
{
"pro_name": "amla hair amla",
"pro_price": "0"
}
]
},
"Amul": {
"items": [
{
"pro_name": "Amul butter",
"pro_price": "48"
},
{
"pro_name": "Amul cow ghee",
"pro_price": "270"
},
{
"pro_name": "Amul Kool Elaine flavored milk",
"pro_price": "20"
},
{
"pro_name": "Amul plain cheese slices",
"pro_price": "122"
},
{
"pro_name": "pure ghee",
"pro_price": "0"
}
]
}

预期的输出应该在嵌套的回收器视图中:

Category Name 1
Product1
Product2
Product3
Category Name 2
Product4
Product5
Product6
...

尝试使用以下代码。

//if your product(Pepsico,Amul...etc) is also dynamic than you can parse you api response in hashmap dynamically
HashMap<String,Product>()
//if your product is fixed then you should use this approach
class ApiResult{
Product Pepsico;
Product Amul;
public Product getPepsico() {
return Pepsico;
}
public void setPepsico(Product pepsico) {
Pepsico = pepsico;
}
public Product getAmul() {
return Amul;
}
public void setAmul(Product amul) {
Amul = amul;
}
}

class  Product{
ArrayList<HashMap<String,String>> items;
public ArrayList<HashMap<String, String>> getItems() {
return items;
}
public void setItems(ArrayList<HashMap<String, String>> items) {
this.items = items;
}
} 

相关内容

  • 没有找到相关文章

最新更新