简体   繁体   中英

Parsing JSON array inside json array in android

I'm getting json from server like in this pattern.

[
   {
      "Response":[
         {
            "CategoryID":1,
            "CategoryName":"Software",
            "Count":1,
            "Tasks":[
               {
                  "ATId":17,
                  "TaskName":"Def",
                  "TaskId":17,
                  "TaskDetails":"FGH"
               }
            ]
         },
         {
            "CategoryID":2,
            "CategoryName":"Hardware",
            "Count":5,
            "Tasks":[
               {
                  "ATId":3,
                  "TaskName":"Def",
                  "TaskId":5,
                  "TaskDetails":"FGH"
               },
               {
                  "ATId":4,
                  "TaskName":"Def",
                  "TaskId":6,
                  "TaskDetails":"FGH"
               },
               {
                  "ATId":6,
                  "TaskName":"Def",
                  "TaskId":6,
                  "TaskDetails":"FGH"
               },
               {
                  "ATId":11,
                  "TaskName":"Def",
                  "TaskId":13,
                  "TaskDetails":"FGH"
               },
               {
                  "ATId":12,
                  "TaskName":"Def",
                  "TaskId":14,
                  "TaskDetails":"FGH"
               }
            ]
         },
         {
            "CategoryID":3,
            "CategoryName":"Web",
            "Count":1,
            "Tasks":[
               {
                  "ATId":13,
                  "TaskName":"Def",
                  "TaskId":11,
                  "TaskDetails":"FGH"
               }
            ]
         },
         {
            "CategoryID":4,
            "CategoryName":"Ios",
            "Count":3,
            "Tasks":[
               {
                  "ATId":5,
                  "TaskName":"Def",
                  "TaskId":7,
                  "TaskDetails":"FGH"
               },
               {
                  "ATId":8,
                  "TaskName":"Def",
                  "TaskId":7,
                  "TaskDetails":"FGH"
               },
               {
                  "ATId":15,
                  "TaskName":"Def",
                  "TaskId":15,
                  "TaskDetails":"FGH"
               }
            ]
         }
      ],
      "MessageStatus":"Success",
      "MessageCode":1
   }
]

here I have array of objects which have inner objects also. So I tried to do it and fetched all the data but this is not in a form as i receive from server. All I need is to get a pattern in which i correctly get it. My java code is here.

@Override
public void onResponse(String response) {

    ParentItems entryObj = null;
    JSONObject jsonObj = null;
    JSONArray jsonArr = null;
    try {
        jsonArr = new JSONArray(response);
        //   JSONArray js=jsonObj.getJSONArray(response);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    String code = null;
    String Message = null;
    try {
        jsonObj = jsonArr.getJSONObject(0);
        code = jsonObj.optString("MessageCode");
        Message = jsonObj.optString("MessageStatus");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    if (code.equals("1")) {
        try {
            Utilities.parentItemsList = new ArrayList < > ();
            Utilities.childItemsList = new ArrayList < > ();
            JSONArray array = jsonObj.getJSONArray("Response");


            for (int i = 0; i < array.length(); i++) {
                JSONObject entryJson = array.getJSONObject(i);
                entryObj = new ParentItems();
                entryObj.CategoryID = entryJson.getInt("CategoryID");
                entryObj.mName = entryJson.getString("CategoryName");
                entryObj.Count = entryJson.getInt("Count");
                Utilities.parentItemsList.add(entryObj);
                JSONArray taskArray = entryJson.optJSONArray("Tasks");

                for (int a = 0; a < taskArray.length(); a++) {
                    ChildItems childItems = new ChildItems();

                    childItems.ATId = entryJson.optInt("ATId");
                    childItems.TaskName = entryJson.optString("TaskName");
                    childItems.TaskId = entryJson.optInt("TaskId");
                    childItems.TaskDetails = entryJson.optString("TaskDetails");
                    Utilities.childItemsList.add(childItems);
                }

            }
        }

    } else {
        Toast.makeText(getContext(), "business ID is null", Toast.LENGTH_LONG).show();

    }
} catch (Exception e) {

    Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();

}

i need my show show data in a expandable listview in a pattern that Catagory object will be parent item and task will be child item

In your ParentItems class. Add another attribute like this:

class ParentItems {
 ...Other attributes
 List<ChildItems> childItems;
}

Then in your parser do something like this:

        for (int i = 0; i < array.length(); i++) {
            JSONObject entryJson = array.getJSONObject(i);
            entryObj = new ParentItems();
            entryObj.CategoryID = entryJson.getInt("CategoryID");
            entryObj.mName = entryJson.getString("CategoryName");
            entryObj.Count = entryJson.getInt("Count");
            entryObj.childItems = new ArrayList<>();
            JSONArray taskArray = entryJson.optJSONArray("Tasks");

            for (int a = 0; a < taskArray.length(); a++) {
                ChildItems childItems = new ChildItems();
                childItems.ATId = entryJson.optInt("ATId");
                childItems.TaskName = entryJson.optString("TaskName");
                childItems.TaskId = entryJson.optInt("TaskId");
                childItems.TaskDetails = entryJson.optString("TaskDetails");
                Utilities.childItemsList.add(childItems);
                entryObj.childItems.add(childItems);
            }
            Utilities.parentItemsList.add(entryObj);

        }

This way you will get all the children tasks as part of the parent object.

PS. For tasks like this, it's easier to use a library like GSON .

You have to make Two Model Class For This like below.

public class ParentItems{
    String CategoryID;
        String CategoryName;
            String Count;
    ArrayList<ChildItems> ChildList;
    // Here Constuctor and Getter Setter
}

public class ChildItems{
        String ATId;
                String TaskName;
                String TaskId;
        String TaskDetails;
// Here Constuctor and Getter Setter
}

you have to add data like this

   for (int i = 0; i < array.length(); i++) {
            JSONObject entryJson = array.getJSONObject(i);
            entryObj = new ParentItems();
            entryObj.CategoryID = entryJson.getInt("CategoryID");
            entryObj.mName = entryJson.getString("CategoryName");
            entryObj.Count = entryJson.getInt("Count");
            Utilities.parentItemsList.add(entryObj);
            JSONArray taskArray = entryJson.optJSONArray("Tasks");

            if(taskArray.length()>0){

                     ArrayList<ChildItems> list=new ArrayList<>();

            for (int a = 0; a < taskArray.length(); a++) {
                ChildItems childItems = new ChildItems();

                childItems.ATId = entryJson.optInt("ATId");
                childItems.TaskName = entryJson.optString("TaskName");
                childItems.TaskId = entryJson.optInt("TaskId");
            childItems.TaskDetails = entryJson.optString("TaskDetails");
                list.add(childItems);
            }
            entryObj.setChildList(list);

}

        }

For More see this link its help you. Expandable Listview Example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM