実現したいこととご質問
googleのapiを利用して、以下のようなJSONの結果が返ってきており、
この中の"duration"=>{"text"=>"33 mins", "value"=>1967},
の33という数字をtimeという変数に代入したいと思っていますが、
JSONを扱うのが初めてで、色々と調べてもいまいちよくわからずにおります・・・
[1] pry(main)> response => {"geocoded_waypoints"=> [{"geocoder_status"=>"OK", "place_id"=>"ChIJ0Qgx67KMGGARd2ZbObLZHPE", "types"=>["locality", "political"]}, {"geocoder_status"=>"OK", "place_id"=>"ChIJWRK3eBSLGGARCdO8OgKkE5w", "types"=>["political", "sublocality", "sublocality_level_2"]}], "routes"=> [{"bounds"=>{"northeast"=>{"lat"=>35.6622678, "lng"=>139.7153187}, "southwest"=>{"lat"=>35.6458126, "lng"=>139.703939}}, "copyrights"=>"Map data ©2019 Google", "legs"=> [{"distance"=>{"text"=>"2.6 km", "value"=>2568}, "duration"=>{"text"=>"33 mins", "value"=>1967}, "end_address"=>"Ebisu, Shibuya City, Tokyo 150-0013, Japan", "end_location"=>{"lat"=>35.6458126, "lng"=>139.7153187}, "start_address"=>"Shibuya City, Tokyo, Japan", "start_location"=>{"lat"=>35.6622678, "lng"=>139.703939}, "steps"=> [{"distance"=>{"text"=>"0.3 km", "value"=>312}, "duration"=>{"text"=>"4 mins", "value"=>261}, "end_location"=>{"lat"=>35.6608076, "lng"=>139.7067344}, "html_instructions"=>"Head <b>east</b>", "polyline"=>{"points"=>"ehtxEs|tsYVgA@ECAh@gCHYBM@EDKBGNWT_@Xg@@EBE|@{A@EVWj@m@"}, "start_location"=>{"lat"=>35.6622678, "lng"=>139.703939}, "travel_mode"=>"WALKING"}, {"distance"=>{"text"=>"6 m", "value"=>6}, "duration"=>{"text"=>"1 min", "value"=>4}, "end_location"=>{"lat"=>35.6607444, "lng"=>139.7068565}, "html_instructions"=>"Turn <b>left</b> onto <b>青山通り</b>/<b>国道246号</b>", "maneuver"=>"turn-left", "polyline"=>{"points"=>"a_txEanusYCEPS"}, "start_location"=>{"lat"=>35.6608076, "lng"=>139.7067344}, "travel_mode"=>"WALKING"}, {"distance"=>{"text"=>"23 m", "value"=>23}, "duration"=>{"text"=>"1 min", "value"=>40}, "end_location"=>{"lat"=>35.660603, "lng"=>139.7069058}, "html_instructions"=>"Cross the road", "polyline"=>{"points"=>"s~sxE{nusY@CPQFJ"}, "start_location"=>{"lat"=>35.6607444, "lng"=>139.7068565}, "travel_mode"=>"WALKING"}, {"distance"=>{"text"=>"0.3 km", "value"=>267}, "duration"=>{"text"=>"3 mins", "value"=>196}, "end_location"=>{"lat"=>35.6587413, "lng"=>139.7085902}, "html_instructions"=>"Turn <b>left</b>", "maneuver"=>"turn-left", "polyline"=>{"points"=>"w}sxEeousYFEXU@Cb@]BC`A{@FGv@s@BD~@y@DGXUb@a@FEHKHI@E"}, "start_location"=>{"lat"=>35.660603, "lng"=>139.7069058}, "travel_mode"=>"WALKING"}, {"distance"=>{"text"=>"0.4 km", "value"=>426}, "duration"=>{"text"=>"5 mins", "value"=>300}, "end_location"=>{"lat"=>35.6553456, "lng"=>139.7065912}, "html_instructions"=>"Turn <b>right</b>", "maneuver"=>"turn-right", "polyline"=>{"points"=>"crsxEuyusYB@HDZNNDFBDBXLPNB@NHJFB@HDDBNBDFfAh@DBLFHDFBf@VNDRJDD\PLDvAr@D@RJh@XFBPJTLPH\R"}, "start_location"=>{"lat"=>35.6587413, "lng"=>139.7085902}, "travel_mode"=>"WALKING"}, {"distance"=>{"text"=>"11 m", "value"=>11}, "duration"=>{"text"=>"1 min", "value"=>8}, "end_location"=>{"lat"=>35.6553467, "lng"=>139.7064737}, "html_instructions"=>"Turn <b>right</b> onto <b>明治通り</b>/<b>都道305号</b>", "maneuver"=>"turn-right", "polyline"=>{"points"=>"}|rxEemusYGHFL"}, "start_location"=>{"lat"=>35.6553456, "lng"=>139.7065912}, "travel_mode"=>"WALKING"}, {"distance"=>{"text"=>"0.2 km", "value"=>192}, "duration"=>{"text"=>"3 mins", "value"=>167}, "end_location"=>{"lat"=>35.6538622, "lng"=>139.7055776}, "html_instructions"=>"Turn <b>left</b> at <b>並木橋(交差点)</b>", "maneuver"=>"turn-left", "polyline"=>{"points"=>"}|rxEmlusYLF?ENJFDDBLFHBB@LBFBF@JDLFFBRJDBLHz@h@j@\PL"}, "start_location"=>{"lat"=>35.6553467, "lng"=>139.7064737}, "travel_mode"=>"WALKING"}, {"distance"=>{"text"=>"0.7 km", "value"=>748}, "duration"=>{"text"=>"9 mins", "value"=>541}, "end_location"=>{"lat"=>35.6482206, "lng"=>139.710175}, "html_instructions"=>"Turn <b>left</b>", "maneuver"=>"turn-left", "polyline"=> {"points"=> "ssrxE{fusY^_@t@m@FLBCl@i@HERQLMJIDEDGBCDGBCBC@AFCBADEFETMBAFABAFCDEFGJGHGd@c@HGLKLKVYZ[DC@AFEPMDEt@i@XQHEPKd@WZO\QvAm@hAi@BADCDCFKTOXQJG@?DEBCFGHIRSBCDE`@_@DGDGDEDIHMLS@A@CBA?AEMX_@"}, "start_location"=>{"lat"=>35.6538622, "lng"=>139.7055776}, "travel_mode"=>"WALKING"}, {"distance"=>{"text"=>"0.6 km", "value"=>583}, "duration"=>{"text"=>"8 mins", "value"=>450}, "end_location"=>{"lat"=>35.6458126, "lng"=>139.7153187}, "html_instructions"=>"Continue straight onto <b>都道305号</b><div style=\"font-size:0.9em\">Destination will be on the left</div>", "maneuver"=>"straight", "polyline"=>{"points"=>"kpqxEscvsYFGt@w@j@q@DIJIJAHSzAaBDGPSFG`@c@HMp@u@FGDEFEFE?K?IDW?OJoALwA@UReCJoADMCCR{B"}, "start_location"=>{"lat"=>35.6482206, "lng"=>139.710175}, "travel_mode"=>"WALKING"}], "traffic_speed_entry"=>[], "via_waypoint"=>[]}], "overview_polyline"=> {"points"=> "ehtxEs|tsYXmACAh@gCLg@FQbAgBdAmBbAeACEPSRUFJFEZYf@a@hAcAv@s@BDdAaA|@w@PQJOx@\LFj@\b@TNHNBDFlAl@VLrAl@b@VdBx@jAj@vAv@GHFLLF?EVPRJb@Lh@RbBbA|@j@tAmAFLBCv@o@r@o@RWx@g@XMbA{@hBaBfBoA|Ay@bEkBJGFKTOd@YFEh@i@p@q@Ze@T[EO`@g@rB}BJAHS`BiBdAmAfAiAFE?KDa@J_BNmB^uEDMCCR{B"}, "summary"=>"都道305号", "warnings"=>["Walking directions are in beta. Use caution – This route may be missing sidewalks or pedestrian paths."], "waypoint_order"=>[]}],
time=何と記載すると、
time=33にすることができるのか、
おわかりになる方がいれば教えていただきたいです。非常に初歩的な質問で申し訳ありません。
質問にアップされているのが JSON 文字列なのですか? JSON であれば http://www.json.org/json-ja.html にあるような形式になると思うのですが・・・
parseした結果(hash)を表示してるんでは
https://docs.ruby-lang.org/ja/latest/class/JSON.html#M_PARSE
こちら、結果を返す為に記載したコードを記載せず、わかりづらく申し訳ありませんでした。
以下のようなコードで返してきた値ですので、gentaroさんのおっしゃられるようにparseしてきた結果となりますm_ _m
uri = URI.escape("https://maps.googleapis.com/maps/api/directions/json?origin=渋谷&destination=恵比寿&mode=walking&key=API key")
res = HTTP.get(uri).to_s
response = JSON.parse(res)
回答2件
あなたの回答
tips
プレビュー