Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSON:在android中从JSON文件获取latlng_Android_Json_Null_Okhttp - Fatal编程技术网

JSON:在android中从JSON文件获取latlng

JSON:在android中从JSON文件获取latlng,android,json,null,okhttp,Android,Json,Null,Okhttp,一个完整的问题编辑,上一个问题由@santalu回答,为你的男人感到荣幸 现在的问题是,我正在尝试访问一个JSON文件,它是我从下面的函数中获得的。我正在创建一个应用程序,列出一个城市中所有的旅游景点及其位置。我能够访问json并读取旅游景点的名称,但我无法从json读取纬度和经度 public void get_json_tourist_places(){ Response responses; responses = null; /*urls=

一个完整的问题编辑,上一个问题由@santalu回答,为你的男人感到荣幸

现在的问题是,我正在尝试访问一个JSON文件,它是我从下面的函数中获得的。我正在创建一个应用程序,列出一个城市中所有的旅游景点及其位置。我能够访问json并读取旅游景点的名称,但我无法从json读取纬度和经度

public void get_json_tourist_places(){
        Response responses;
        responses = null;
        /*urls="https://maps.googleapis.com/maps/api/place/textsearch/json?query="+place_name+"point+of+interest&language=en&type=tourist_attraction,museum,amusement_park,zoo"+"&key="+getString(R.string.api_key);
        */
        urls = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=new+york+city+point+of+interest&language=en&key=API_key&type=tourist_attraction,museum,amusement_park,zoo";
        try {
            Log.d("JSON", "about to retrieve data");
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(urls)
                    .build();


            try {
                responses = client.newCall(request).execute();
                jsonData = responses.body().string();
                Log.d("JSON", jsonData);
                /*return response_ztring;*/
                if (jsonData != null) {
                    Log.d("JSON", "data received");
                    JSONObject Jobject = null;
                    try {

                        Jobject = new JSONObject(jsonData);
                        Log.d("JSON", "Jobject read jsondata");

                    } catch (JSONException e) {
                        Log.d("JSON", "Jobject from jsonData error");
                        e.printStackTrace();
                    }

                    JSONArray Jarray = null;
                    try {

                        Jarray = Jobject.getJSONArray("results");
                        Log.d("JSON", "JArray read results");
                    } catch (JSONException e) {
                        Log.d("JSON", "Jobject from jsonData error");
                        e.printStackTrace();
                    }

                    if (Jarray != null) {
                        /*Jarralength = Jarray.length();*/
                        Jarralength = 19;

                        for (int i = 0; i < Jarralength; i++) {
                            try {
                                JSONObject object = Jarray.getJSONObject(i);
                                tourist_name = object.getString("name");
                                Log.d("JSON",tourist_name);
                                tourist_lat = object.getDouble("lat");
                                Log.d("JSON",Double.toString(tourist_lat));
                                tourist_lng = object.getDouble("lng");
                                tourist_latlang = new LatLng(tourist_lat,tourist_lng);
                                mMap.addMarker(new MarkerOptions().position(tourist_latlang).title(tourist_name));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }}
                }else{
                    Log.d("JSON", "Null object found");
                }


            } catch (IOException e) {
                Log.d("JSON", "inside function - error 1");
                Log.d("JSON", e.toString()+"inside function");
                /*return null;*/
            }

        }catch (Exception e){
            Log.d("JSON", "inside function - error 2");
            Log.d("JSON",e.toString()+"inside function");
            /*return null;*/
        }






    }
}
我从api收到的json文件如下:-

{
       "html_attributions" : [],
       "next_page_token" : "CtQERAIAAF_ny-R_r3JBJMToZ6eVAv3cu9Ang5GnRFKNDml2JnE0OmsRxDCWeO0tslc46gW6Hl65jLD-eEpcf1Zaoyhaw5LKl3dojxMqZJx48SMazCjw0V5dkMqU1r8yMfvOITUhqJvtpJGxSKlxEMRke9tN5rxnp8oG-6FGCwR1oRrx_h-zichjyKUintGJJBiDKnTXBQVIscePKScKDSKM0xxkZnX0didAm8meOA7jR7Wte6HRNn0VEslgnlpekJuhi5dAguOE-nk4UuOG6MCuSAMQxhqCYYVDxKa8QZKBv7RoElcDuoezTTCYo3yjRliEIFTfCi0vMybBvZHGmGlY5cRQMshxMsJKnnpsFrl-5zPmsUvuhPe8cVkE-isfN1WiG7uUfiDWj_bVpUp7JOKrh81aqTnW4zIEGzT6nSoS87ltXyinqlY2heVxInLoYaKpvUNaeghkTr0cGL5sdgLu1RGUKHr6zkJrDY63I51jkAOZsYaYHEEMvnSB-JUE6VTT_04Kg7JRMLO6VbEQnP3tJsbLuzRm4K7rbo07WggKYLzpgk7cMtFQpqzcTUExTiJVhmG8aBvij58s9eLbhXROoZN9gDifhBg3d-jqNPHw0-RXgHMD6blK_DxBeUwj1Ndij7ekaRLjouhTtStOWMbWkdZmfzS1XffY6ceKrAHKmacpoTH0p8QYGTw5v5WPfi-O4DOfBqFMvw60HGJX3IEBb3yqpxGnz9K5nm2qJ1W2-LxEQ-5Nn7VjbeYLCGsw9W3nE87zuagwBjPOF3RjYnfurkkF4QASEI6zwqeQiFUDF67KOq-LT-oaFOc4TT7-o5btr0CCiNFGoU3GPYAq",
       "results" : [
          {
             "formatted_address" : "New York, NY 10011, United States",
             "geometry" : {
                "location" : {
                   "lat" : 40.7479925,
                   "lng" : -74.0047649
                },
                "viewport" : {
                   "northeast" : {
                      "lat" : 40.75890824999998,
                      "lng" : -73.99853175
                   },
                   "southwest" : {
                      "lat" : 40.73287364999999,
                      "lng" : -74.01141114999999
                   }
                }
             },
             "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_recreational-71.png",
             "id" : "72dc79d023feec77725d7336e7c46fee27805319",
             "name" : "The High Line",
             "opening_hours" : {
                "open_now" : true
             },
             "photos" : [
                {
                   "height" : 3267,
                   "html_attributions" : [
                      "\u003ca href=\"https://maps.google.com/maps/contrib/105487997458497097901\"\u003eАлександр Иванов\u003c/a\u003e"
                   ],
                   "photo_reference" : "CmRaAAAAMQLLPJRDK2RUdpgd5HM7cCyLO0n4JPecWoRECfD1-0tGtjtxj3kDrv4bcGVML6Ka6h6lUBuJ1qA0lfhDe_ZnLl9krJCjEr1caXLUdNpPRxgQrgdGhHKkmK6XBWlq-Nd7EhCJMH4b-vQfZ9agTO6C73AvGhQihQAwG1zkF2nZygrixAD4NOzaZw",
                   "width" : 4895
                }
             ],
             "place_id" : "ChIJ5bQPhMdZwokRkTwKhVxhP1g",
             "plus_code" : {
                "compound_code" : "PXXW+53 New York, United States",
                "global_code" : "87G7PXXW+53"
             },
             "rating" : 4.7,
             "reference" : "ChIJ5bQPhMdZwokRkTwKhVxhP1g",
             "types" : [ "park", "tourist_attraction", "point_of_interest", "establishment" ],
             "user_ratings_total" : 40128
          },
          {
             "formatted_address" : "1260 6th Ave, New York, NY 10020, United States",
             "geometry" : {
                "location" : {
                   "lat" : 40.75997599999999,
                   "lng" : -73.97997719999999
                },
                "viewport" : {
                   "northeast" : {
                      "lat" : 40.76128112989272,
                      "lng" : -73.97878962010728
                   },
                   "southwest" : {
                      "lat" : 40.75858147010728,
                      "lng" : -73.98148927989271
                   }
                }
             },
             "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
             "id" : "89dd7f0c82efc14f18954f8abf2925515037927a",
             "name" : "Radio City Music Hall",
             "photos" : [
                {
                   "height" : 2988,
                   "html_attributions" : [
                      "\u003ca href=\"https://maps.google.com/maps/contrib/116446596751419338697\"\u003eAndres Moya\u003c/a\u003e"
                   ],
                   "photo_reference" : "CmRaAAAAa2L9ceDCY9787WSlvpuvWntfsA60hBed8bquDMUaXuCCjXdvjLx5neVliD1aRJlkcdTvjElMuc_jhN0z2fYFjS9mUnACdRw-HVgVGRmpsMTFmi_dB-FG1Wx3BII3ZEOYEhBJY7yVQgUzoCwy5vh61NM1GhTy52brtjjvEgMVRTzmZ7vE0LRUlQ",
                   "width" : 5312
                }
             ],
             "place_id" : "ChIJPS8b1vhYwokRldqq2YHmxJI",
看看你的JSON:

{
       ...
       "results" : [
          {
             ...
             "geometry" : {
                "location" : {
                   "lat" : 40.7479925,
                   "lng" : -74.0047649
                },
要获得
lat
,需要向下遍历这些对象

你开始,你做到了:

Jobject = new JSONObject(jsonData);
Jobject.getJSONArray("results");
但你需要继续这样做:

Jobject.getJSONArray("results")
.getJSONObject("geometry")
.getJSONObject("location")
.getDouble("lat")
JSONObject object = Jarray.getJSONObject(i);
tourist_name = object.getString("name");
Log.d("JSON",tourist_name);

JSONObject location = object.getJSONObject("geometry").getJSONObject("location");                                
tourist_lat = location.getDouble("lat");                                
tourist_lng = location.getDouble("lng");
Log.d("JSON", "Lat/Lng" + tourist_lat + "/" + tourist_lng);
也就是这样的:

Jobject.getJSONArray("results")
.getJSONObject("geometry")
.getJSONObject("location")
.getDouble("lat")
JSONObject object = Jarray.getJSONObject(i);
tourist_name = object.getString("name");
Log.d("JSON",tourist_name);

JSONObject location = object.getJSONObject("geometry").getJSONObject("location");                                
tourist_lat = location.getDouble("lat");                                
tourist_lng = location.getDouble("lng");
Log.d("JSON", "Lat/Lng" + tourist_lat + "/" + tourist_lng);

有个小错误。。。只需将
if(jsonData==null)
替换为
if(jsonData!=null)
即可从每个数组对象中获取
“几何体”
的JSONObject,然后从该数组中获取
“lat”/“lng”的字符串,类似地,获取
“视口”的JSONObject
非常感谢,伙计!!