ruby上でGoogle Maps Direction APIを叩き,経路情報を取得し,徒歩での移動時間を取得したいと考えています.
そこで,下のコードでjsonをhashにparseしましたが,深い箇所がparse出来ず,参照出来なくて困っています.
legs以下の値をruby上で取得する方法をご教授願えると幸いです.
ruby
1# APIから得たjsonデータをhashにparse 2data = JSON.parse(response.body) 3puts data["routes"] 4puts data["routes"]["legs"] # Error : no implicit conversion of String into Integer
ruby
1# parse後 2"geocoded_waypoints"=>[ 3 { 4 "geocoder_status"=>"OK", 5 "place_id"=>"ChIJC6fjlY3mAGARSshZ6CLIrhs", 6 "types"=>[ 7 "establishment", 8 "point_of_interest", 9 "train_station", 10 "transit_station" 11 ] 12 }, 13 { 14 "geocoder_status"=>"OK", 15 "place_id"=>"ChIJ67mcWJLmAGARrUf0FlFtm7w", 16 "types"=>[ 17 "department_store", 18 "establishment", 19 "point_of_interest", 20 "store" 21 ] 22 } 23], 24"routes"=>[ 25 { 26 "bounds"=>{ 27 "northeast"=>{ 28 "lat"=>34.7029859, 29 "lng"=>135.4985137 30 }, 31 "southwest"=>{ 32 "lat"=>34.7023575, 33 "lng"=>135.4959768 34 } 35 }, 36 "copyrights"=>"地図データ ©2018 Google, ZENRIN", 37 "legs"=>[ 38 { 39 "distance"=>{ 40 "text"=>"0.3 km", 41 "value"=>343 42 }, 43 "duration"=>{ 44 "text"=>"5分", 45 "value"=>303
json
1{ 2 "geocoded_waypoints": [ 3 { 4 "geocoder_status": "OK", 5 "place_id": "ChIJC6fjlY3mAGARSshZ6CLIrhs", 6 "types": [ 7 "establishment", 8 "point_of_interest", 9 "train_station", 10 "transit_station" 11 ] 12 }, 13 { 14 "geocoder_status": "OK", 15 "place_id": "ChIJ67mcWJLmAGARrUf0FlFtm7w", 16 "types": [ 17 "department_store", 18 "establishment", 19 "point_of_interest", 20 "store" 21 ] 22 } 23 ], 24 "routes": [ 25 { 26 "bounds": { 27 "northeast": { 28 "lat": 34.7029859, 29 "lng": 135.4985137 30 }, 31 "southwest": { 32 "lat": 34.7023575, 33 "lng": 135.4959768 34 } 35 }, 36 "copyrights": "地図データ ©2018 Google, ZENRIN", 37 "legs": [ 38 { 39 "distance": { 40 "text": "0.3 km", 41 "value": 343 42 }, 43 "duration": { 44 "text": "5分", 45 "value": 303 46 }, 47〜以下略〜
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。