如何在不使用密钥的情况下解析包含JSON的对象和JSON数组?

问题描述 投票:0回答:1

我有一个JSON响应,我必须在活动中显示。我遇到的问题是响应中存在的JSON数组。我想解析它并显示活动数据,作为响应“start_times”数组我想一次只显示一个数据...

这是我从服务器获得的JSON响应

{
    "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
            }
        ]
         }
}

我的代码:

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

      }

    }
  }

实际结果是=

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"]]

预期结果(我想)是=

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

任何想法如何解决这个问题?

java android json parsing
1个回答
0
投票

这是更正后的代码。在for循环中使用timeTableTime数组

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


                    }
热门问题
推荐问题
最新问题