我有一个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
任何想法如何解决这个问题?
答案 0 :(得分: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);
}