0

I have this kind of json data returning from url as shown in Image1 Image2 Image3 Basically there are dates inside data and then within these dates there are further 5 different things i.e. session_from, session_to, rate, bookingFound and promotion. What i want is that i want to store all these dates data in separate arraylist.

For example:

sessionfrom0 contains data for 09-09-2018 and all its objects

sessionfrom1 contains data for 10-09-2018 and all its objects

sessionfrom2 contains data for 11-09-2018 and all its objects

I have tried with following piece of code but its not working correctly

JSONObject jsonObject = new JSONObject(response);
JSONObject dataObj = jsonObject.getJSONObject("data");
Iterator<String> iter = dataObj.keys();

    sessionsfrom0 = new ArrayList<String>();
    sessionsfrom1 = new ArrayList<String>();
    sessionsfrom2 = new ArrayList<String>();
        while (iter.hasNext()) {
            key = iter.next();
            JSONArray datesArray = dataObj.getJSONArray(key);

            for (int i = 0; i < datesArray.length(); i++) {
                 JSONObject datesObject0 = datesArray.getJSONObject(i);
                 JSONObject datesObject1 = datesArray.getJSONObject(i);
                 JSONObject datesObject2 = datesArray.getJSONObject(i);

                 sessionsfrom0.add(datesObject0.getString("session_from") + " - " +datesObject0.getString("session_to");
                 sessionsfrom1.add(datesObject1.getString("session_from") + " - " +datesObject1.getString("session_to");
                 sessionsfrom2.add(datesObject2.getString("session_from") + " - " +datesObject2.getString("session_to");
                  } }

This code not working correctly, as you can see that date 09-09-2018 contains further array of size 4 and and in those array there are further 5 items inside each jjson object so i want to store all this in first arraylist i.e. sessionfrom0 and then go to next date and pick all its arrays and json objects and store data in sessionfrom1 arraylist and so on.

3 Answers 3

1

Try this:

JSONObject jsonObject = new JSONObject(response);
JSONObject dataObj = jsonObject.getJSONObject("data");
Iterator<String> iter = dataObj.keys();

sessionsfrom0 = new ArrayList<String>();
sessionsfrom1 = new ArrayList<String>();
sessionsfrom2 = new ArrayList<String>();
    while (iter.hasNext()) {
        String key = iter.next();
        JSONArray datesArray = dataObj.getJSONArray(key);

        switch (key) {
           case "2018-09-09":
              fillSessions(datesArray, sessionsfrom0);
              break;

           case "2018-10-09":
              fillSessions(datesArray, sessionsfrom1);
              break;
           ....so on....
        }
    }

You can create a function instead of rewriting the for loop logic on each case:

void fillSessions(JSONArray jsonArray, List<String> sessionList) {
   for (int i = 0; i < jsonArray.length(); i++) {
      JSONObject datesObject = jsonArray.getJSONObject(i);

      sessionList.add(datesObject.getString("session_from") + " - " +datesObject0.getString("session_to");
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I have been stuck on this from few hours. You are genious. Works like a charm. Thankyou so much :)
No problem, happy to help :)
1

You can do this by using HashMap

JSONObject jsonObject = new JSONObject(response);
    JSONObject dataObj = jsonObject.getJSONObject("data");
    Iterator<String> iter = dataObj.keys();

    HashMap<String, ArrayList<String>> map = new HashMap<>();

    while (iter.hasNext()) {
        key = iter.next();

        JSONArray datesArray = dataObj.getJSONArray(key);
        ArrayList<String> list = new ArrayList<>();

        for (int i = 0; i < datesArray.length(); i++) {
            JSONObject datesObject0 = datesArray.getJSONObject(i);

            list.add(datesObject0.getString("session_from") + " - " + datesObject0.getString("session_to");
        }
        map.put(key, list);
        }

1 Comment

Thankyou @prashant17. I have got the answer. Although this is correct too so upvote for this too :)
1

Try this:

JSONObject jsonObject = new JSONObject(response);
JSONObject dataObj = jsonObject.getJSONObject("data");
Iterator<String> iter = dataObj.keys();

Map<String, ArrayList<String>> map = new TreeMap<>();
while (iter.hasNext()) {
    String key = iter.next();
    JSONArray datesArray = dataObj.getJSONArray(key);
    ArrayList<String> tmp = new ArrayList<>();
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject datesObject = jsonArray.getJSONObject(i);
        tmp.add(datesObject.getString("session_from") + " - "+datesObject.getString("session_to"));
    }
    map.put(key, tmp);
}

Probably you don't have all keys inside data so using switch case is not sensitive.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.