Json对象字段在android中为数字

时间:2014-05-05 08:51:17

标签: android json parsing

{
"All Day Menu":[{
    "id": "2",
    "description": "",
    "image": "",
    "name": "Veg",
    "menu_id": "1",
    "categories": [{
        "id": "822",
        "description": "",
        "image": "",
        "name": "Veg Items",
        "menu_mcat_id": "2",
        "items": {
            "631836": {
                "standard": {
                    "id": "631836",
                    "restid": "4806",
                    "menu_id": "1",
                    "name": "Shahi paneer",
                    "menu_cat_id": "822",
                    "description": null,
                    "image": "",
                    "posItemID": "0",
                    "video_url": "",
                    "option_id": "1195420",
                    "price": "160",
                    "start_date": "2007-06-01",
                    "end_date": null,
                    "start_time": "00:00:00",
                    "end_time": "23:59:00",
                    "has_options": "0",
                    "calories": null,
                    "packaging": "0",
                    "size": "standard",
                    "popular": null
                }
            },
            "631838": {
                "standard": {
                    "id": "631838",
                    "restid": "4806",
                    "menu_id": "1",
                    "name": "Kali mirch paneer",
                    "menu_cat_id": "822",
                    "description": null,
                    "image": "",
                    "posItemID": "0",
                    "video_url": "",
                    "option_id": "1195422",
                    "price": "160",
                    "start_date": "2007-06-01",
                    "end_date": null,
                    "start_time": "00:00:00",
                    "end_time": "23:59:00",
                    "has_options": "0",
                    "calories": null,
                    "packaging": "0",
                    "size": "standard",
                    "popular": null
                }
            }
        }
    }]
}, {
    "id": "4",
    "description": "",
    "image": "",
    "name": "Non-Veg",
    "menu_id": "1",
    "categories": [{
        "id": "9078",
        "description": "",
        "image": "",
        "name": "Chicken Items",
        "menu_mcat_id": "4",
        "items": {
            "1246892": {
                "standard": {
                    "id": "1246892",
                    "restid": "4806",
                    "menu_id": "1",
                    "name": "Butter chicken",
                    "menu_cat_id": "9078",
                    "description": null,
                    "image": "",
                    "posItemID": "0",
                    "video_url": "",
                    "option_id": "1195424",
                    "price": "380",
                    "start_date": "2007-06-01",
                    "end_date": null,
                    "start_time": "00:00:00",
                    "end_time": "23:59:00",
                    "has_options": "0",
                    "calories": null,
                    "packaging": "0",
                    "size": "standard",
                    "popular": null
                }
            },
            "1246894": {
                "standard": {
                    "id": "1246894",
                    "restid": "4806",
                    "menu_id": "1",
                    "name": "Kadhai chicken",
                    "menu_cat_id": "9078",
                    "description": null,
                    "image": "",
                    "posItemID": "0",
                    "video_url": "",
                    "option_id": "1195426",
                    "price": "380",
                    "start_date": "2007-06-01",
                    "end_date": null,
                    "start_time": "00:00:00",
                    "end_time": "23:59:00",
                    "has_options": "0",
                    "calories": null,
                    "packaging": "0",
                    "size": "standard",
                    "popular": null
                }
            },
            "1246896": {
                "standard": {
                    "id": "1246896",
                    "restid": "4806",
                    "menu_id": "1",
                    "name": "Masala chicken",
                    "menu_cat_id": "9078",
                    "description": null,
                    "image": "",
                    "posItemID": "0",
                    "video_url": "",
                    "option_id": "1195428",
                    "price": "380",
                    "start_date": "2007-06-01",
                    "end_date": null,
                    "start_time": "00:00:00",
                    "end_time": "23:59:00",
                    "has_options": "0",
                    "calories": null,
                    "packaging": "0",
                    "size": "standard",
                    "popular": null
                }
            }
        }
    }]
}, {
    "id": "12",
    "description": "",
    "image": "",
    "name": "Roti-Naan",
    "menu_id": "1",
    "categories": [{
        "id": "16490",
        "description": "",
        "image": "indian-breads-roti02-jpg",
        "name": "Indian Breads",
        "menu_mcat_id": "12",
        "items": {
            "1540012": {
                "standard": {
                    "id": "1540012",
                    "restid": "4806",
                    "menu_id": "1",
                    "name": "Chapati",
                    "menu_cat_id": "16490",
                    "description": null,
                    "image": "",
                    "posItemID": "0",
                    "video_url": "",
                    "option_id": "1195430",
                    "price": "8",
                    "start_date": "2007-06-01",
                    "end_date": null,
                    "start_time": "00:00:00",
                    "end_time": "23:59:00",
                    "has_options": "0",
                    "calories": null,
                    "packaging": "0",
                    "size": "standard",
                    "popular": null
                }
            },
            "1540014": {
                "standard": {
                    "id": "1540014",
                    "restid": "4806",
                    "menu_id": "1",
                    "name": "Butter chapati",
                    "menu_cat_id": "16490",
                    "description": null,
                    "image": "",
                    "posItemID": "0",
                    "video_url": "",
                    "option_id": "1195432",
                    "price": "15",
                    "start_date": "2007-06-01",
                    "end_date": null,
                    "start_time": "00:00:00",
                    "end_time": "23:59:00",
                    "has_options": "0",
                    "calories": null,
                    "packaging": "0",
                    "size": "standard",
                    "popular": null
                }
            },
            "1540016": {
                "standard": {
                    "id": "1540016",
                    "restid": "4806",
                    "menu_id": "1",
                    "name": "Naan",
                    "menu_cat_id": "16490",
                    "description": null,
                    "image": "",
                    "posItemID": "0",
                    "video_url": "",
                    "option_id": "1195434",
                    "price": "20",
                    "start_date": "2007-06-01",
                    "end_date": null,
                    "start_time": "00:00:00",
                    "end_time": "23:59:00",
                    "has_options": "0",
                    "calories": null,
                    "packaging": "0",
                    "size": "standard",
                    "popular": null
                }
            },
            "1540018": {
                "standard": {
                    "id": "1540018",
                    "restid": "4806",
                    "menu_id": "1",
                    "name": "Butter naan",
                    "menu_cat_id": "16490",
                    "description": null,
                    "image": "",
                    "posItemID": "0",
                    "video_url": "",
                    "option_id": "1195436",
                    "price": "30",
                    "start_date": "2007-06-01",
                    "end_date": null,
                    "start_time": "00:00:00",
                    "end_time": "23:59:00",
                    "has_options": "0",
                    "calories": null,
                    "packaging": "0",
                    "size": "standard",
                    "popular": null
                }
            },
            "1540020": {
                "standard": {
                    "id": "1540020",
                    "restid": "4806",
                    "menu_id": "1",
                    "name": "Laccha paratha",
                    "menu_cat_id": "16490",
                    "description": null,
                    "image": "",
                    "posItemID": "0",
                    "video_url": "",
                    "option_id": "1195438",
                    "price": "25",
                    "start_date": "2007-06-01",
                    "end_date": null,
                    "start_time": "00:00:00",
                    "end_time": "23:59:00",
                    "has_options": "0",
                    "calories": null,
                    "packaging": "0",
                    "size": "standard",
                    "popular": null
                }
            },
            "1540022": {
                "standard": {
                    "id": "1540022",
                    "restid": "4806",
                    "menu_id": "1",
                    "name": "Kali mirch laccha paratha",
                    "menu_cat_id": "16490",
                    "description": null,
                    "image": "",
                    "posItemID": "0",
                    "video_url": "",
                    "option_id": "1195440",
                    "price": "30",
                    "start_date": "2007-06-01",
                    "end_date": null,
                    "start_time": "00:00:00",
                    "end_time": "23:59:00",
                    "has_options": "0",
                    "calories": null,
                    "packaging": "0",
                    "size": "standard",
                    "popular": null
                }
            }
        }
     }]
}}
}

我正在使用来自android docs的内置Json库。我一直在成功解析直到category数组,之后我被困在这里:{"数值"。我创建了一个物品对象,但之后我无法检索到价值。我应该如何处理这个任何帮助将不胜感激。 P.S不能使用Jackson或Gson解析器,因为其他一切都依赖于我正在使用的当前解析器。

2 个答案:

答案 0 :(得分:0)

我不完全确定我理解你的问题,但是你可以使用keys方法获得一个迭代器,它允许你枚举"项目中的键。对象

代码看起来与此类似:

JSONObject items = ...;
Iterator itemIter = items.keys();
while (itemIter.hasNext()) {
    String key = (String)itemIter.next();
    JSONObject item = items.getJSONObject(key);
    ...
}

编辑:

如果它为您提供Enumeration,则您很可能不会使用org.json。尝试与Iterator交换Enumeration

JSONObject items = ...;
Enumeration itemEnum = items.keys();
while (itemEnum.hasMoreElements()) {
    String key = (String)itemEnum.nextElement();
    JSONObject item = items.getJSONObject(key);
    ...
}

答案 1 :(得分:0)

您可以使用迭代器来解析动态生成的json:以下是我用于项目的代码:

JSONObject responseJsonObject = obj.getJSONArray("items");
int size = responseJsonObject.length();
for(int i = 0;i<size;i++){
Jsonobject temp = responseJsonObject.getItemAtIndex(i);
Iterator keys = temp.keys();
        while (keys.hasNext()) {
            String curKey = (String) keys.next();
            String curValue = responseJsonObject.getString(curKey);
            AppData.listCityId.add(curKey);
            AppData.listCityName.add(curValue);
            // Log.d("key", curKey+ "  "+curValue);
        }

}

请注意,我只展示了如何解析动态json。