简体   繁体   中英

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

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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