【问题标题】:How to parse JSON containing object and JSON Array without key?如何在没有键的情况下解析包含对象和 JSON 数组的 JSON?
【发布时间】:2019-04-16 07:07:00
【问题描述】:

我有一个 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】:

    这是更正后的代码。在你的 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);
    }
    

    【讨论】:

    • 这对我有用,谢谢我现在明白了。但是我的输出显示为 Time : ["08:00:00","09:00:00"] 使用 String time = hero.getTime().toString();任何想法@Deepak Khillare
    • 是的,您需要拆分字符串数组并使用- 分隔符连接它们。因此,您可以执行timeTableTime.getJSONArray(i) 而不是timetableTime.getJSONObject(i),这将返回数组。然后你可以使用timeObject[0] - timeObject[1] 来获得想要的结果。
    • timeTableTime.getJSONArray(i).join("-");
    猜你喜欢
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多