Android 如何为这种类型的json数据创建pojo类,因为在这种类别和子类别中,只有通过id才能组合关系
在这个json中,我的类别和子类别是组合的,如何在pojo类中设置,如何在可扩展列表视图中设置,请帮助Android 如何为这种类型的json数据创建pojo类,因为在这种类别和子类别中,只有通过id才能组合关系,android,Android,在这个json中,我的类别和子类别是组合的,如何在pojo类中设置,如何在可扩展列表视图中设置,请帮助 { "product_categories": [{ "id": 24, "name": "Air Cooled", "slug": "air-cooled", "parent": 67, "description": "", "di
{
"product_categories": [{
"id": 24,
"name": "Air Cooled",
"slug": "air-cooled",
"parent": 67,
"description": "",
"display": "subcategories",
"image": "",
"count": 4
}, {
"id": 33,
"name": "Belt Driven",
"slug": "bet-driven",
"parent": 25,
"description": "",
"display": "default",
"image": "",
"count": 15
}, {
"id": 25,
"name": "CNC LATHE SPINDLE",
"slug": "cnclathespindle",
"parent": 0,
"description": "",
"display": "default",
"image": "",
"count": 15
}, {
"id": 28,
"name": "CNC MILLING SPINDLE",
"slug": "cncmillingspindle",
"parent": 0,
"description": "",
"display": "default",
"image": "",
"count": 9
}, {
"id": 29,
"name": "Motorised",
"slug": "singles",
"parent": 25,
"description": "",
"display": "default",
"image": "",
"count": 0
}, {
"id": 21,
"name": "MOTORIZED SPINDLE",
"slug": "motorizedspindle",
"parent": 0,
"description": "",
"display": "default",
"image": "",
"count": 11
}, {
"id": 30,
"name": "Water Cooled",
"slug": "water-cooled",
"parent": 67,
"description": "",
"display": "subcategories",
"image": "",
"count": 7
}, {
"id": 67,
"name": "Wood Acrylic Engraving",
"slug": "wood-acrylic-engraving",
"parent": 21,
"description": "",
"display": "products",
"image": "",
"count": 0
}]
}
在这个json中,我的类别和子类别是组合的,如何在pojo类中设置,我不想在可扩展列表视图中设置,请帮助您必须解析json响应并将不同的类别存储在不同的arraylist中,根据用户对渲染位置的选择,您可以使用所选的arraylist 您可以通过像这样解析json响应来实现这一点
JSONObject jsonObject = new JSONObject(response); // response is the json string that you are gettting from server
JSONArray jsonArray = jsonObject.getJSONObject("product_categories");
ArrayList<Categorydisplay> catDisList = new ArrayList<>();
ArrayList<Defaultdisplay> defaultDisList = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
SomeModel someModel = new SomeModel();
if (jsonObject.has("id")) {
someModel.setId(jsonObject.getString("id"));
}
if (jsonObject.has("name")) {
someModel.setName(jsonObject.getString("name"));
}
so on.....
if(someModel.getDisplay().equals("default")){
defaultDisList.add(someModel);
}else{
catDisList.add(someModel);
}
}
JSONObject JSONObject=新的JSONObject(响应);//response是从服务器获取的json字符串
JSONArray JSONArray=jsonObject.getJSONObject(“产品类别”);
ArrayList catDisList=新的ArrayList();
ArrayList defaultDisList=新建ArrayList();
for(int i=0;i
检查此项