【问题标题】:how to get lng and lat from JSON file如何从 JSON 文件中获取 lng 和 lat
【发布时间】:2013-01-21 18:53:03
【问题描述】:

我想从我的 JSON 文件中获取“lat”和“lng”并添加到一个保存 GeoPoint 的数组中。我试图这样做,但它对我不起作用:

protected List<GeoPoint> JsonArray(){
        List<GeoPoint> endp = new ArrayList<GeoPoint>();

        try{
        JSONObject obj = new JSONObject(json);
        JSONArray steps = obj.getJSONArray("routes");
        for(int i=0;i<steps.length();i++){
            JSONObject temp = steps.getJSONObject(i);
            JSONObject ele = temp.optJSONObject("steps").optJSONObject("end_location");
            ele.getJSONObject("lat");
            ele.getJSONObject("lng");

            double lat = Double.parseDouble(ele.getJSONObject("lat").toString());
            double lng = Double.parseDouble(ele.getJSONObject("lng").toString());
            endp.add(new GeoPoint((int)(lat *1E6),(int)(lng * 1E6)));

        }



        }catch (JSONException e) {
            // TODO: handle exception
        }
        return endp;
    }

这是我的 JSON 文件的一部分外观:

{
       "routes" : [
          {
             "bounds" : {
                "northeast" : {
                   "lat" : 41.87999000000001,
                   "lng" : -87.615020
                },
                "southwest" : {
                   "lat" : 29.74674000000001,
                   "lng" : -95.361220
                }
             },
             "copyrights" : "Dane do Mapy ©2013 Google",
             "legs" : [
                {
                   "distance" : {
                      "text" : "1 085 mil",
                      "value" : 1746457
                   },
                   "duration" : {
                      "text" : "16 godz. 39 min",
                      "value" : 59955
                   },
                   "end_address" : "1362 Chenevert Street, Houston, Teksas 77003, Stany Zjednoczone",
                   "end_location" : {
                      "lat" : 29.750110,
                      "lng" : -95.36016000000001
                   },
                   "start_address" : "138-230 South Columbus Drive, Chicago, Illinois 60601, Stany Zjednoczone",
                   "start_location" : {
                      "lat" : 41.87999000000001,
                      "lng" : -87.62075000000002
                   },
                   "steps" : [
                      {
                         "distance" : {
                            "text" : "338 stóp",
                            "value" : 103
                         },
                         "duration" : {
                            "text" : "1 min",
                            "value" : 9
                         },
                         "end_location" : {
                            "lat" : 41.88090,
                            "lng" : -87.62069000000001
                         },
                         "html_instructions" : "Kieruj się \u003cb\u003eS Columbus Dr\u003c/b\u003e na \u003cb\u003epółnoc\u003c/b\u003e w stronę \u003cb\u003eE Monroe St\u003c/b\u003e",
                         "polyline" : {
                            "points" : "}tr~FtlxuOuA@w@@QMUA"
                         },
                         "start_location" : {
                            "lat" : 41.87999000000001,
                            "lng" : -87.62075000000002
                         },
                         "travel_mode" : "DRIVING"
                      },
                      {
                         "distance" : {
                            "text" : "0,2 mil",
                            "value" : 266
                         },
                         "duration" : {
                            "text" : "1 min",
                            "value" : 33
                         },
                         "end_location" : {
                            "lat" : 41.88086000000001,
                            "lng" : -87.61750000000001
                         },
                         "html_instructions" : "Skręć \u003cb\u003ew prawo\u003c/b\u003e w \u003cb\u003eE Monroe St\u003c/b\u003e",
                         "polyline" : {
                            "points" : "szr~FhlxuO?SAyA?_B@yA?iBAaB?WBE@C@A?C?A@A?C?y@?_@"
                         },
                         "start_location" : {
                            "lat" : 41.88090,
                            "lng" : -87.62069000000001
                         },
                         "travel_mode" : "DRIVING"
                      },
                      {
                         "distance" : {
                            "text" : "2,0 mil",
                            "value" : 3186
                         },
                         "duration" : {
                            "text" : "3 min",
                            "value" : 180
                         },
                         "end_location" : {
                            "lat" : 41.85320,
                            "lng" : -87.61470000000001
                         },
                         "html_instructions" : "Skręć \u003cb\u003ew prawo\u003c/b\u003e w \u003cb\u003eU.S. 41 S\u003c/b\u003e",
                         "polyline" : {
                            "points" : "kzr~FjxwuOpMOZ@`PO|ACvDC@?lDCZAfMGjAA`@Av@?^?P?N@P@j@LRFNDRJRHZRb@\\b@^nAjA@@JJXXb@b@z@v@RRp@`@RLr@Xn@Np@J`A@bBOp@Kv@K@?@?@A@?@?@?@?PEvBWxB]fDq@xA]jF_BTIj@QpGuBd@OBAf@SrDyAPGNG@AVKbA_@~@]|@]z@Y`F{AtCy@fHyB"
                         },
                         "start_location" : {
                            "lat" : 41.88086000000001,
                            "lng" : -87.61750000000001
                         },
                         "travel_mode" : "DRIVING"
                      },
                      {
                         "distance" : {
                            "text" : "0,4 mil",
                            "value" : 581
                         },
                         "duration" : {
                            "text" : "1 min",
                            "value" : 30
                         },
                         "end_location" : {
                            "lat" : 41.848390,
                            "lng" : -87.614670
                         },
                         "html_instructions" : "Zjedź \u003cb\u003eInterstate 55 S\u003c/b\u003e w kierunku \u003cb\u003eSaint Louis\u003c/b\u003e",
                         "polyline" : {
                            "points" : "omm~FzfwuOVDPAN?VGlBk@b@M@?JEDAVI`A[dBi@`A[z@UPGZGf@E\\Ab@@VDJBHBTFTHRJPNRNLL\\f@NXLTP\\BF"
                         },
                         "start_location" : {
                            "lat" : 41.85320,
                            "lng" : -87.61470000000001
                         },
                         "travel_mode" : "DRIVING"
                      },

有谁知道我该怎么做?

【问题讨论】:

  • 请发布您的完整有效 json
  • 我无法在此处添加所有内容,但这里有指向我的 json 文件的链接 maps.google.com/maps/api/directions/…
  • Android MapView 不使用 Google Maps API V3。 (已删除标签)

标签: android json google-maps


【解决方案1】:

您的变量 JSONArray steps = obj.getJSONArray("routes"); 不代表 JSON 对象的“步骤”部分,而是代表“路线”部分。

此外,您还跳过了 JSON 层次结构中的一些级别。使用给定的 JSON,您将需要以下步骤来遍历 JSON(我省略了对不同数组的循环,除了 routes 数组)

JSONArray routes = obj.getJSONArray("routes");
for(int i=0; i < routes.length(); i++){
    // Grab the first route
    JSONObject route = routesArray.getJSONObject(i);
    // Take all legs from the route
    JSONArray legs = route.getJSONArray("legs");
    // Grab first leg
    JSONObject leg = legs.getJSONObject(0);
    // Take all steps from the leg
    JSONArray steps = leg.getJSONArray("steps");
    // Grab first step
    JSONObject step = steps.getJSONObject(0);

    JSONObject endLocation = step.getJSONObject("end_location");
    String lat = endLocation.getString("lat");
    String lng = endLocation.getString("lng");

参考:JSON parsing of Google Maps API in Android App

【讨论】:

    【解决方案2】:

    解析当前的 json 字符串以获得 lat 和 lng :

    JSONObject obj = new JSONObject(json);
            JSONArray steps = obj.getJSONArray("routes");
            for(int i=0;i<steps.length();i++){
                JSONObject temp = steps.getJSONObject(i);
    
                 // get bounds JSONObject 
                JSONObject boundsjsonobj = temp.getJSONObject("bounds");
                 // get northeast JSONObject
                JSONObject jsonboj_bounds_northeast= boundsjsonobj.getJSONObject("northeast");
                // get northeast lng and lat 
                 String str_northeast_lat=jsonboj_bounds_northeast.getString("lat");
                 String str_northeast_lng=jsonboj_bounds_northeast.getString("lng");
                 // get southwest JSONObject
                 JSONObject jsonboj_bounds_southwest= boundsjsonobj.getJSONObject("southwest");
                // get northeast lng and lat 
                 String str_southwest_lat=jsonboj_bounds_southwest.getString("lat");
                 String str_southwest_lng=jsonboj_bounds_southwest.getString("lng");
    
                 //get legs JsonArray from routes
                 JSONArray jsonarray_legs = temp.getJSONArray("legs");
                 for(int j=0;j<jsonarray_legs.length();j++){
                    JSONObject jobjlegs = jsonarray_legs.getJSONObject(j);
                     // get end_location json object
    
                     JSONObject jobjlegs_end_location = jobjlegs.getJSONObject("end_location");
                      String str_end_location_lat==jobjlegs_end_location.getString("lat");
                      String str_end_location_lng==jobjlegs_end_location.getString("lng");
    
                      // get start_address object
    
    
                     JSONObject jobjlegs_start_address = jobjlegs.getJSONObject("start_address");
                      String str_start_address_lat==jobjlegs_start_address.getString("lat");
                      String str_start_address_lng==jobjlegs_start_address.getString("lng");
    
                      // get steps jsonArray
                      JSONArray jsonarray_steps = jobjlegs.getJSONArray("steps");
                      for(int k=0;k<jsonarray_steps.length();k++){
                    JSONObject jobjsteps = jsonarray_steps.getJSONObject(k);
    
                        // get end_location jsonobject
                         JSONObject jobjsteps_end_location = jobjsteps.getJSONObject("end_location");
                           double latend = Double.parseDouble(jobjsteps_end_location.getString("lat"));
                           double lngend = Double.parseDouble(jobjsteps_end_location.getString("lng"));
                           endp.add(new GeoPoint((int)(latend *1E6),(int)(lngend * 1E6)));
    
                         // get start_location jsonobject
                         JSONObject jobjsteps_start_location = jobjsteps.getJSONObject("start_location");
                           double latstart = Double.parseDouble(jobjsteps_start_location.getString("lat"));
                           double lngstart = Double.parseDouble(jobjsteps_start_location.getString("lng"));
                      }
    
                 }
    
            }
    

    【讨论】:

    • 这部分我做错了 JSONObject obj = new JSONObject(json);因为当我进行调试时,它从这里直接到异常。我需要以任何特殊方式将链接放入我的 JSON 文件吗?打开这个或某事?
    • @carolajnn :给我你的 json 的 url,因为解析是正确的,这里是 new JSONObject(json);您需要传递从 google place api 而不是 json 检索到的字符串
    • 我想通了。我使用了错误的 JSONParser,但我改变了,现在工作正常 :)
    猜你喜欢
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    相关资源
    最近更新 更多