Parse Json Array Inside Json Array with android

5.2k views Asked by At

I'm trying to make extended list view with android and this is my json result .

[
  {
    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: [

    ]
  }
]

and my android code is :

 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);

But i have an exception and have a problem with children list . I have tried alot of things but they didn't work with me , please can any body help me to find the wrong thing with my code ?

2

There are 2 answers

1
Ankit Popli On BEST ANSWER

Try something like this:

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);          
}
0
Sanket990 On

I have create one method for your response parsing checking. Successfully parse your jsonarray response also getting sub-category response.

Check Below method and parse your json array sting in below method. if any issue then add comment

 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();
        }
    }

I am getting below log and successfully parse your json response without any error

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