0

I have a JSON response which I have to show on activity. The problem I'm having is with the JSON Array that is present in the response. I want to parse it and show the data on the activity, in response the "start_times" array I want to show only one data at a time...

This is the JSON response i'm getting from the server

{
    "data": {
        "start_times": [
            [
                "08:00:00",
                "09:00:00"
            ],
            [
                "09:00:00",
                "10:00:00"
            ],
            [
                "10:00:00",
                "11:00:00"
            ]
        ],
        "mon": [
            {
                "subject__name": Electronics,
                "faculty__first_name": Manoj
            },
            {
                "subject__name": null,
                "faculty__first_name": null
            },
            {
                "subject__name": null,
                "faculty__first_name": null
            }
        ]
         }
}

My code:

  final StringRequest myStringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>()
  {

    @Override
    public void onResponse(String response)
    {
      Log.i(TAG, "Response-->" + response);
      System.out.println(response);
      try
      {
        JSONObject obj = new JSONObject(response);
        JSONObject obj2 = obj.getJSONObject("data");
        JSONArray timetable = obj2.getJSONArray("mon");

        JSONArray timeTableTime = obj2.getJSONArray("start_times");
        Log.d(TAG, "timeTableTime-->" + timeTableTime);

        Log.d(TAG, "TimetableLength-->" + timetable.length());
        for (int i = 0; i < timetable.length(); i++)
        {
          JSONObject heroObject = timetable.getJSONObject(i);

          mondayHero mon = new mondayHero(
              heroObject.getString("faculty__first_name"),
              heroObject.getString("subject__name"),
              heroObject.getString("faculty__first_name"),
              obj2.getJSONArray("start_times"));
          Log.d(TAG, "mon-->" + mon);
          mondayList.add(mon);

        }

        //creating custom adapter object
        mondayListViewAdaptor adapter = new mondayListViewAdaptor(mondayList, c.getApplicationContext());

        //adding the adapter to listview
        listView.setAdapter(adapter);

      }

    }
  }

Actual result is =

Subject : Electronics
Faculty : Manoj
Time : [["08:00:00","09:00:00"],
       ["09:00:00","10:00:00"],
       ["10:00:00","11:00:00"]]

Subject : null
Faculty : null
Time : [["08:00:00","09:00:00"],
       ["09:00:00","10:00:00"],
       ["10:00:00","11:00:00"]]

Subject : null
Faculty : null
Time : [["08:00:00","09:00:00"],
       ["09:00:00","10:00:00"],
       ["10:00:00","11:00:00"]]

Expected Result(i want) is =

Subject : Electronics
Faculty : Manoj
Time : 08:00:00-09:00:00

Subject : null
Faculty : null
Time : 09:00:00-10:00:00

Subject : null
Faculty : null
Time : 10:00:00-11:00:00

Any Idea how to solve this problem??

1 Answer 1

1

Here's the corrected code. Use the timeTableTime array in your for loop

for (int i = 0; i < timetable.length(); i++) {
    JSONObject heroObject = timetable.getJSONObject(i);
    JSONObject timeObject = timetableTime.getJSONObject(i);

    mondayHero mon = new mondayHero(
        heroObject.getString("faculty__first_name"),
        heroObject.getString("subject__name"),
        heroObject.getString("faculty__first_name"),
        timeObject);
    Log.d(TAG,"mon-->"+mon);
    mondayList.add(mon);
}
Sign up to request clarification or add additional context in comments.

3 Comments

This is working for me, thanks i got the idea now. But my output is showing as Time : ["08:00:00","09:00:00"] after using String time = hero.getTime().toString(); any idea @Deepak Khillare
Yes, you'll need to split the string array and join them using - separator. So instead of doing timetableTime.getJSONObject(i) you can do timeTableTime.getJSONArray(i) which will return you the array. And then you can use timeObject[0] - timeObject[1] for getting the desired result.
timeTableTime.getJSONArray(i).join("-");

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.