【问题标题】:JSON parse multidimensional array volleyJSON解析多维数组凌空
【发布时间】:2018-08-06 22:04:45
【问题描述】:

我在我的 Android 应用程序中尝试解析 JSON 时偶然发现了一个问题。在下面的屏幕截图中,我提供了 JSON 的结构。除了获得“几何”JSONObject 之外,我无法走得更远。最终,我需要一个包含每个“功能”的 LatLng 的数组。

如果有人能指出我正确的方向,我将不胜感激。

这是我的代码。

 //gets the area's out of the JSON
    JsonObjectRequest LosLoopJson = new JsonObjectRequest(losloopURL, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {

                JSONArray features = response.getJSONArray("features");

                for (int i = 0; i < features.length(); i++) {
                    JSONObject Area = features.getJSONObject(i);

                    for (int x = 0; x < features.length(); x++) {
                        JSONObject geometry = Area.getJSONObject("geometry");
                        Double[][][] coordinates = geometry.get("coordinates");


                    }

                }



            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("Volley", "Error");
                }
            }
    );

    requestQueue.add(objectrequest);

这是我的 JSON 结构:

提前致谢!

【问题讨论】:

  • 邮政编码,不是图片。另外提示:用英语而不是荷兰语、变量名、cmets 等做所有事情
  • 请将您的代码发布为文本而不是屏幕截图。这样,其他用户可以复制您的代码并自己尝试。至于你的问题;你可以制作JSONArray 类型的coordinates 并循环你的LatLongs

标签: java json multidimensional-array


【解决方案1】:

坐标是一个json数组,其中的每个元素,也包含一个json数组。所以你必须遍历坐标对象。

JSONArray coordinates = geometry.get("coordinates");
for(int i = 0; i < coordinates.size(); i++)
    JSONArray coord   = coordinates.get(i);

【讨论】:

    【解决方案2】:

    试试这个:-

    try {
            JSONObject jsonObject = new JSONObject(jsonString);
            JSONArray jsonArray = jsonObject.getJSONArray("features");
            for (int i = 0; i <= jsonArray.length(); i++) {
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                if (jsonObject1.has("geomerty")){
                    JSONArray jsonArray1 = jsonObject1.getJSONArray("geomerty");
                    for (int j = 0; j <= jsonArray1.length(); j++){
                        if (jsonObject1.has("coordinates")){
                            JSONArray jsonArray2 = jsonObject1.getJSONArray("coordinates");
                            for (int k = 0; k <= jsonArray2.length();k++){
                                JSONArray jsonArray3 = jsonArray2.getJSONArray(k);
                                for (int l =0; l <= jsonArray3.length(); l++){
                                    Log.d("ABC", String.valueOf(jsonArray3.getDouble(l)));
                                }
    
                            }
                        }
                    }
                }
    
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    

    【讨论】:

      【解决方案3】:

      感谢所有帮助,我使用此代码使其正常工作:

       //gets the area's out of the JSON
          JsonObjectRequest LosLoopJson = new JsonObjectRequest(losloopURL, new Response.Listener<JSONObject>() {
              @Override
              public void onResponse(JSONObject response) {
                  try {
      
                      JSONArray features = response.getJSONArray("features");
      
                      for (int i = 0; i < features.length(); i++) {
                          JSONObject Area = features.getJSONObject(i);
                          JSONObject geometry = Area.getJSONObject("geometry");
                          JSONArray coordinates = geometry.getJSONArray("coordinates");
                          JSONArray AreaCoordinates = coordinates.getJSONArray(0);
      
                          //Log.d("Debug", "Feature: " + String.valueOf(i + 1));
      
                              for (int y = 0; y < AreaCoordinates.length(); y++) {
                                  //Log.d("Debug", "Coordinate: " + String.valueOf(y + 1));
      
                                  JSONArray DoubleCoordinate = AreaCoordinates.getJSONArray(y);
                                  Double latitude = DoubleCoordinate.getDouble(1);
                                  Double longitude = DoubleCoordinate.getDouble(0);
                                  LatLng coordinate = new LatLng(latitude, longitude);
      
                                  LosLoopGebied.add(coordinate);
                              }
      
                              losloopgebieden.add(LosLoopGebied);
                              LosLoopGebied.clear();
      
                      }
      
      
                  //PlaatsLosLoopGebieden();
                      //Log.d("Debug", "Heeft de losloopgebieden opgehaalt uit JSON");
                      //Log.d("Debug", "Er zijn " + String.valueOf(LosLoopGebied.size()) + " coordinaten opgehaalt");
                      Log.d("Debug", "Er zijn " + String.valueOf(losloopgebieden.size()) + " opgehaalt uit JSON");
      
      
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
              }
          },
                  new Response.ErrorListener() {
                      @Override
                      public void onErrorResponse(VolleyError error) {
                          Log.e("Volley", "Error");
                      }
                  }
          );
      
          requestQueue.add(LosLoopJson);
      

      【讨论】:

        猜你喜欢
        • 2011-01-30
        • 1970-01-01
        • 1970-01-01
        • 2022-01-19
        • 2014-07-04
        • 2014-02-08
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        相关资源
        最近更新 更多