簡體   English   中英

如何解析沒有對象的JSON包含對象和JSON數組?

[英]How to parse JSON containing object and JSON Array without key?

我有一個必須在活動中顯示的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

任何想法如何解決這個問題?

這是更正的代碼。 在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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM