简体   繁体   English

如何从排球响应中读取数组内部数组中的数组?

[英]How to read an array inside an array inside an array from a volley response?

I'm using volley to make a request to the server, the server respond with a multidimensional array, and i'm trying to read the "second" array "details" that are inside one show; 我正在使用凌空向服务器发出请求,服务器使用多维数组进行响应,而我正在尝试读取一个节目内的“第二个”数组“详细信息”;

This is what im using to read the response: 这就是我用来阅读回复的内容:

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();
    }
}

I tryed doing whit inside the loop, but it did'n work :/ 我尝试在循环中做白衣,但它确实无效:/

 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,
    }
]

You can try below code : 你可以尝试下面的代码:

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) ;
      }    
     }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM