如何使用凌空解析android中的json数组 [英] how to parse json array in android using volley
本文介绍了如何使用凌空解析android中的json数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取此JSON数组,但未得到结果.有人可以告诉我如何使用排球吗?我需要预订中的所有数据.请帮助我实现它.
I am trying to fetch this JSON array but didn't get the result. Can someone please tell me how can I do using volley. I need all the data within the booking. Please help me to achieve it.
{
"code": 200,
"savebooking": [
{
"Booking": {
"id": "304",
"contributor_id": "16",
"table_of_content_id": "6791",
"composition_id": "7469",
"completion": "2017-06-30",
"approved": null,
"cdn_id": "328301423",
"secret_token": "s-riQMc",
"uploaded_on": "2017-06-16 07:51:35",
"created": "2017-06-16 07:48:31",
"modified": "2017-06-16 07:51:35"
}
},
{
"Booking": {
"id": "305",
"contributor_id": "16",
"table_of_content_id": "6791",
"composition_id": "7470",
"completion": "2017-06-30",
"approved": null,
"cdn_id": "328318377",
"secret_token": "s-naSse",
"uploaded_on": "2017-06-16 10:43:39",
"created": "2017-06-16 07:48:31",
"modified": "2017-06-16 10:43:39"
}
},
{
"Booking": {
"id": "306",
"contributor_id": "16",
"table_of_content_id": "6791",
"composition_id": "7471",
"completion": "2017-06-30",
"approved": null,
"cdn_id": null,
"secret_token": null,
"uploaded_on": null,
"created": "2017-06-16 07:48:31",
"modified": "2017-06-16 07:48:31"
}
},
{
"Booking": {
"id": "307",
"contributor_id": "16",
"table_of_content_id": "6791",
"composition_id": "7472",
"completion": "2017-06-30",
"approved": null,
"cdn_id": null,
"secret_token": null,
"uploaded_on": null,
"created": "2017-06-16 07:48:31",
"modified": "2017-06-16 07:48:31"
}
}
]
}
如何使用凌空解析此JSON响应? 我已经尝试过了,但是没有得到结果.
How can I parse this JSON response using volley? I have tried this but didn't get the result.
StringRequest postStringRequest = new StringRequest(Request.Method.POST, BOOK_API,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Response Check :" + response);
try {
JSONObject json = new JSONObject(response);
JSONObject booking = json.getJSONArray("savebooking").getJSONObject("Booking");
Log.d(TAG, "booking Response Check :" + booking);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
推荐答案
这里是
//Here is main response object
JSONObject json = new JSONObject(response);
//now get your json array like this
JSONArray booking = json.getJSONArray("savebooking");
//现在检查数组长度> 0
// now check the array length > 0
if(booking.length ()> 0){
for(int countItem = 0;countItem<booking.length;countItem++){
JSONObject bookingObject = booking.getJsonObject(countItem);
String id = bookingObject.isNull("id")?"":bookingObject.optString("id");
list.add(id);
}
}
这篇关于如何使用凌空解析android中的json数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文