获取Json数据时出错

时间:2014-12-28 13:03:21

标签: android json

这是我的json数据

{"CategoryID":1,"CategoryName":"Clothing","Status":true,"SubCategoryList":    [{"Status":true,"SubCategoryID":1,"SubCategoryName":"Male"},    {"Status":true,"SubCategoryID":2,"SubCategoryName":"Female"},    {"Status":true,"SubCategoryID":3,"SubCategoryName":"Kids"},    {"Status":true,"SubCategoryID":4,"SubCategoryName":"New Born Baby"},    {"Status":true,"SubCategoryID":11,"SubCategoryName":"Sun Glasses"}]},    {"CategoryID":2,"CategoryName":"Alcohol","Status":true,"SubCategoryList":    [{"Status":true,"SubCategoryID":5,"SubCategoryName":"Wine"},    {"Status":true,"SubCategoryID":6,"SubCategoryName":"Beer"},    {"Status":true,"SubCategoryID":7,"SubCategoryName":"Scotch"}]},    {"CategoryID":3,"CategoryName":"Restourents","Status":false,"SubCategoryList":   [{"Status":true,"SubCategoryID":8,"SubCategoryName":"Breakfast"},   {"Status":true,"SubCategoryID":9,"SubCategoryName":"Tour Package"}]}]

我的代码:

JSONArray collection = new JSONArray(json1);
int length = collection.length();
for(int i=0;i<length;i++){
JSONObject row = collection.getJSONObject(i);
String value = row.getString("CategoryName");
String value6 = row.getString("SubCategoryName");
}

获取CategoryName的值:Clothing,Alcohol,Restourents 无法获取SubCategoryName的值 解析数据时出错org.json.JSONException:SubCategoryName没有值
如何获取SubCategoryName的数据。

1 个答案:

答案 0 :(得分:0)

SubCategoryName位于SubCategoryList jsonArray

JsonArray arr = row.getJsonArray("SubCategoryList");
arr.getJsonObject[0].getString("SubCategoryName");