我使用排球向服务器发送请求,服务器使用多维数组进行响应, 而我正在尝试读取一场演出中的“第二个”数组“细节”;
这是即时通讯用来读取响应的内容:
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, params, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("Shows");
Log.e(TAG, "Response Array: " + jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++) {
} catch (JSONException e) {
e.printStackTrace();
}
}
我尝试在循环中进行白色处理,但是没有成功:/
JSONArray arr = new JSONArray(jsonArray);
for (int e = 0; e < arr.length(); e++) {
Log.e(TAG, "INSIDE");
}
"Shows" : [
{
"details" : [
"id" : 23adda,
"date" : "Monday",
"time" : "5:00PM"
"details: [
"Address" : "123 street";
"City" : "Test"
]
],
"id" : 15sdsd,
"Heading" : "The Big Show",
"Category" : "Family show",
"AssetId" : 8c8be292,
}
{
"details" : [
"id" : 23adda,
"date" : "Monday",
"time" : "5:00PM"
],
"id" : 15sdsd,
"Heading" : "The Big Show",
"Category" : "Family show",
"AssetId" : 8c8be292,
}
]
答案 0 :(得分:0)
您可以尝试以下代码:
JSONArray jsonArray = response.getJSONArray("Shows");//getting array
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonobject= jsonArray.getJSONObject(i);//getting first element
String id= jsonobject.getString("id");//getting id
String Heading= jsonobject.getString("Heading");//getting Heading
String Category= jsonobject.getString("Category");//getting Category
String AssetId= jsonobject.getString("AssetId");//getting AssetId
System.out.println(""+id+""+Heading+""+AssetId+""+Category) ;
JSONArray jsonObject1= jsonobject.getJSONArray("details"); //getting children array
for (int j = 0; j < jsonObject1.length(); j++) {
JSONObject object1 = jsonObject1.getJSONObject(j);
String id1= object1.getString("id");//getting id
String date= object1.getString("date");//getting date
String time= object1.getString("time");//getting time
System.out.println(""+id1+""+City+""+time) ;
JSONArray jsonObject2= object1.getJSONArray("details"); //getting children array
for (int k = 0; k < jsonObject2.length(); k++) {
JSONObject object2 = jsonObject2.getJSONObject(k);
String Address= object2.getString("Address");//getting Address
String City= object2.getString("City");//getting City
System.out.println(""+Address+""+City) ;
}
}
}