【问题标题】:Android Studio Volley JSON REST API ParsingAndroid Studio Volley JSON REST API 解析
【发布时间】:2019-04-15 14:55:19
【问题描述】:

编写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":[]}]}]}

【问题讨论】:

  • 在此处发布问题之前,请遵循“如何提问”指南:stackoverflow.com/help/how-to-ask 如果您的问题不符合“如何提问”指南,您可以尝试初学者编程论坛和小组,这将比所以。如果您的代码有精确的技术问题,请将其发布在 SO 上。如果您不遵循 SO 问题指南,您的问题可能会被否决或关闭,没有任何收获或回应。另请参阅:idownvotedbecau.se/noresearch

标签: android json android-volley


【解决方案1】:

您需要将您的 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

        }
        } 

【讨论】:

    猜你喜欢
    • 2019-04-03
    • 2016-08-04
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    相关资源
    最近更新 更多