简体   繁体   中英

How to parse JSON containing object and JSON Array without key?

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??

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

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