使用android解析Json数组里面的Json数组

时间:2015-06-23 16:34:44

标签: android arrays json expandablelistview expandablelistadapter

我试图用android制作扩展列表视图,这是我的json结果。

[
  {
    eCode: 0,
    Menu_Cat_Id: 1,
    Menu_Cat_Name: "Salaaad",
    photo: "http://awtuts.com/restaurant/uploads/categories/2.jpg",
    subCategories: [
      {
        Item_Id: 1,
        Item_NameAR: "شيش طاووق",
        Item_NameEN: "SHeeeeeeesh",
        Item_size: "Larg",
        Item_Prc: 100,
        Item_Photo: "http://awtuts.com/restaurant/uploads/subcategories/1.jpg"
      },
      {
        Item_Id: 1,
        Item_NameAR: "شيش طاووق",
        Item_NameEN: "SHeeeeeeesh",
        Item_size: "Small",
        Item_Prc: 10,
        Item_Photo: "http://awtuts.com/restaurant/uploads/subcategories/1.jpg"
      }
    ]
  },
  {
    eCode: 0,
    Menu_Cat_Id: 2,
    Menu_Cat_Name: "Salaaad",
    photo: "http://awtuts.com/restaurant/uploads/categories/2.jpg",
    subCategories: [

    ]
  }
]

我的android代码是:

 protected void onPostExecute(String result) {
            try {
                Parent recievedUser;
                Parent child;
                ArrayList<Parent> listDataHeader = new ArrayList<Parent>();
                List<String> parents = new ArrayList<>();
                ArrayList<Parent> listChildData = new ArrayList<Parent>();
                List<String> children = new ArrayList<>();
                JSONArray jsonArray = new JSONArray(result);
                JSONArray jsonArrayChild=new JSONArray(result);
                for (int i = 0; i < jsonArray.length(); i++) {
                    jsonObject = jsonArray.getJSONObject(i);
                    recievedUser = new Parent();
                    child=new Parent();
                    recievedUser.setMenu_Cat_Id(jsonObject.getInt("Menu_Cat_Id"));
                    recievedUser.setMenu_Cat_Name(jsonObject.getString("Menu_Cat_Name"));
                    recievedUser.setPhoto(jsonObject.getString("photo"));
                    child.setItem_NameEN(jsonObject.getString("Item_NameEN"));
                    listDataHeader.add(recievedUser);
                    listChildData.add(child);

                }

                // set list adapter
                ExpandableListAdapter expandableListAdapter = new ExpandableListAdapter(MainActivity.this, listDataHeader, listChildData);
                expListView.setAdapter(expandableListAdapter);

但我有一个例外并且有儿童名单的问题。 我已经尝试过很多东西,但他们没有和我合作,请问我的代码可以帮我找到错误的东西吗?

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

JSONArray jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
  jsonObject = jsonArray.getJSONObject(i);
  recievedUser = new Parent();
  recievedUser.setMenu_Cat_Id(jsonObject.getInt("Menu_Cat_Id"));        
  recievedUser.setMenu_Cat_Name(jsonObject.getString("Menu_Cat_Name"));
  recievedUser.setPhoto(jsonObject.getString("photo"));
  categories = new JSONArray(jsonObject.get("categories"));

  for (int i = 0; i < categories.length(); i++) {
   category = categories.getJSONobject(i);
   child=new Parent();
   child.setItem_NameEN(categories.getString("Item_NameEN"));
   listChildData.add(child);  
  }

  listDataHeader.add(recievedUser);          
}

答案 1 :(得分:0)

我为您的响应解析检查创建了一个方法。成功解析您的jsonarray响应也获得子类别响应。

检查下面的方法并用下面的方法解析你的json数组sting。如有任何问题,请添加评论

 public void parseJsonResponse(String response) {
        try {

            JSONArray jsonArray = new JSONArray(response);

            if (jsonArray != null && jsonArray.length() > 0) {
                for (int cnt = 0; cnt < jsonArray.length(); cnt++) {
                    JSONObject jsonObj = null;
                    jsonObj = jsonArray.getJSONObject(cnt);
                    Log.d("tag", "jsonObj Menu_Cat_Id->" + jsonObj.optString("Menu_Cat_Name"));
                    JSONArray jsonArrSubCat = jsonObj.getJSONArray("subCategories");
                    if (jsonArrSubCat != null && jsonArrSubCat.length() > 0) {

                        for (int subCnt = 0; subCnt < jsonArrSubCat.length(); subCnt++) {
                            JSONObject jsonObjSubCategory = jsonArrSubCat.getJSONObject(subCnt);
                            Log.d("tag", "jsonArrSubCat Item_NameEN->" + jsonObjSubCategory.optString("Item_NameEN"));
                        }
                    }
                }

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

我越过日志并成功解析你的json响应而没有任何错误

06-24 00:32:39.205  24881-24881/? D/tag﹕ jsonObj Menu_Cat_Id->Salaaad
06-24 00:32:39.205  24881-24881/? D/tag﹕ jsonArrSubCat Item_NameEN->SHeeeeeeesh
06-24 00:32:39.205  24881-24881/? D/tag﹕ jsonArrSubCat Item_NameEN->SHeeeeeeesh
06-24 00:32:39.205  24881-24881/? D/tag﹕ jsonObj Menu_Cat_Id->Salaaad