Android Studio Volley JSON REST API 解析

Android Studio Volley JSON REST API Parsing

正在编写Android应用程序,需要解析JSON嵌套的数据 以下是从 http 收到的实际数据。

目前正在使用 Volley,

JSONArray dataJSONArray = response.getJSONArray("data");

需要示例代码来解析和显示来自下面提到的 JSON 数据

的数据

{"data":[{"counting_area_id":3,"name":"Utilization","parking_area_id":1, "free":3,"total":200,"location_latitude":null,"location_longitude":null,"places":10, "children":[{"counting_area_id":1,"name":"Basement 1","parking_area_id":1, "free":0,"total":116,"location_latitude":null,"location_longitude":null,"places":0, "children":[]},{"counting_area_id":73,"name":"Basement 2","parking_area_id":1, "free":3,"total":121,"location_latitude":null,"location_longitude":null,"places":3, "children":[]}]}]}

您需要将 data 传递给 JSONArray 并使用 for loop 解析它,如下面的代码:

 JSONArray jsonArray = response.getJSONArray("data");//getting array
            for (int i = 0; i < jsonArray.length(); i++) {
             JSONObject jsonobject= jsonArray.getJSONObject(i);//getting first element 
           String id= jsonobject.getString("counting_area_id");//value of counting_area_id ,get all value in same way i.e location,places etc.
          System.out.println(id);
             JSONArray jsonObject1= object.getJSONArray("children"); //getting children array
     for (int j = 0; j < jsonObject1.length(); j++) {
         JSONObject object1 = jsonObject1.getJSONObject(j);
           String id= object1.getString("counting_area_id");//same as before

        }
        }