我正在使用可扩展列表视图来显示服务器中的类别和子类别。但是我认为我的代码中的JSON解析存在一些问题,因为所有子类别都添加到了每个类别中。我将举例说明,以更好地理解该问题。例如,我有2个主要类别,每个类别有3个子类别,因此它应该显示带有子类别1,2,3的category1和带有子类别4,5,6的category2。但是在我的代码中,这显示的是-具有子类别的category1 1,2,3,4,5,6和类别2及其子类别1,2,3,4,5,6。 这是JSON解析代码-
try
{
JSONObject jsonrootobjct=new JSONObject(response);
JSONArray jsonArray = null;
jsonArray=jsonrootobjct.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = null;
jsonObject1= jsonArray.getJSONObject(i);
try {
category_id = jsonObject1.getString(Json_categoryid);
category_name = jsonObject1.getString(Json_categoryname);
category_item = jsonObject1.getString(Json_categoryitem);
JSONArray jsonArrayitem=null;
jsonArrayitem = jsonObject1.getJSONArray("itemdata");
for (int j = 0; j < jsonArrayitem.length(); j++) {
JSONObject jsonObject11 = null;
jsonObject11 = jsonArrayitem.getJSONObject(j);
try {
item_name = jsonObject11.getString(Json_itemname);
item_id = jsonObject11.getString(Json_itemid);
DayBookDetailsData ch = new DayBookDetailsData(item_name, item_id);
ch_list.add(ch);
}
catch (JSONException e) {
e.printStackTrace();
}
}
DayBookData gru = new DayBookData(category_id,category_name,ch_list);
list.add(gru);
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter=new DayBookAdapter(getActivity(), list);
elv.setAdapter(adapter);
}
这是myJSon数据的样本-
{"data":[{"c_name":"Stage And Platform","cat_id":"29","itemdata":[{"id":"2","item":" Wooden Stage"},{"id":"3","item":" Iron Stage"},{"id":"4","item":"Round Stage"},{"id":"5","item":" Rotating Stage"},{"id":"6","item":" Hydraulic Stage"},{"id":"7","item":"Hydro-Rotating Stage"},{"id":"8","item":"Wooden Dance Floor"},{"id":"9","item":"Acrylic Dance Floor"},{"id":"10","item":"Led Dance Floor"},{"id":"466","item":"Podium"}]}
请让我知道我的代码有什么问题。提前致谢。