#現状のコード
lang
1$response = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=京都駅'); 2$dec = json_decode($response); 3 4return dd( $response );
#実現できるのか
現状のコードから以下のようにしてlatを取り出すことができません。
$response->geometry->location->lat
図れるエラー内容は「Trying to get property of non-object」です。
オブジェクトがないということなのでしょうが、上記の正しい記法がわかりません。
正しく取り出すにはどのようにすればよろしいでしょうか。
JSONの状態
JSONは以下のようになっています。
lang
1""" 2{\n 3 "results" : [\n 4 {\n 5 "address_components" : [\n 6 {\n 7 "long_name" : "Kyoto Station",\n 8 "short_name" : "Kyoto Station",\n 9 "types" : [ "point_of_interest", "establishment" ]\n 10 },\n 11 {\n 12 "long_name" : "Higashishiokōji Kamadonochō",\n 13 "short_name" : "Higashishiokōji Kamadonochō",\n 14 "types" : [ "sublocality_level_1", "sublocality", "political" ]\n 15 },\n 16 {\n 17 "long_name" : "Shimogyō-ku",\n 18 "short_name" : "Shimogyō-ku",\n 19 "types" : [ "ward", "locality", "political" ]\n 20 },\n 21 {\n 22 "long_name" : "Kyōto-shi",\n 23 "short_name" : "Kyōto-shi",\n 24 "types" : [ "locality", "political" ]\n 25 },\n 26 {\n 27 "long_name" : "Kyōto-fu",\n 28 "short_name" : "Kyōto-fu",\n 29 "types" : [ "administrative_area_level_1", "political" ]\n 30 },\n 31 {\n 32 "long_name" : "Japan",\n 33 "short_name" : "JP",\n 34 "types" : [ "country", "political" ]\n 35 },\n 36 {\n 37 "long_name" : "600-0000",\n 38 "short_name" : "600-0000",\n 39 "types" : [ "postal_code" ]\n 40 }\n 41 ],\n 42 "formatted_address" : "Kyoto Station, Higashishiokōji Kamadonochō, Shimogyō-ku, Kyōto-shi, Kyōto-fu 600-0000, Japan",\n 43 "geometry" : {\n 44 "location" : {\n 45 "lat" : 34.985849,\n 46 "lng" : 135.7587667\n 47 },\n 48 "location_type" : "APPROXIMATE",\n 49 "viewport" : {\n 50 "northeast" : {\n 51 "lat" : 34.9871979802915,\n 52 "lng" : 135.7601156802915\n 53 },\n 54 "southwest" : {\n 55 "lat" : 34.98450001970851,\n 56 "lng" : 135.7574177197085\n 57 }\n 58 }\n 59 },\n 60 "place_id" : "ChIJ7wKLka4IAWARCByidG5EGrY",\n 61 "types" : [\n 62 "transit_station",\n 63 "train_station",\n 64 "subway_station",\n 65 "point_of_interest",\n 66 "establishment"\n 67 ]\n 68 }\n 69 ],\n 70 "status" : "OK"\n 71}\n 72"""
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。