2017-07-18 182 views
0

請告訴我,我現在如何才能獲得緯度& lon? -從json獲取經緯度

#<HTTParty::Response:0x7fbda4055780 parsed_response={"ip"=>"74.125.232.229", "city"=>{"id"=>5375480, "lat"=>37.38605, "lon"=>-122.08385, "name_ru"=>"Маунтин-Вью", "name_en"=>"Mountain View", "name_de"=>"Mountain View", "name_fr"=>"Mountain View", "name_it"=>"Mountain View", "name_es"=>"Mountain View", "name_pt"=>"Mountain View", "okato"=>"", "vk"=>0, "population"=>74066}} 

回答

1

假設你分配給這樣的變量您HTTParty迴應:

r = #<HTTParty::Response:0x7fbda4055780 parsed_response={"ip"=>"74.125.232.229", "city"=>{"id"=>5375480, "lat"=>37.38605, "lon"=>-122.08385, "name_ru"=>"Маунтин-Вью", "name_en"=>"Mountain View", "name_de"=>"Mountain View", "name_fr"=>"Mountain View", "name_it"=>"Mountain View", "name_es"=>"Mountain View", "name_pt"=>"Mountain View", "okato"=>"", "vk"=>0, "population"=>74066}} 

然後你就可以抓住解析響應(我喜歡它冷漠的訪問),像這樣:

r = r.parsed_response.with_indifferent_access 

然後你就可以這樣做:

lat = r[:city][:lat] 
lon = r[:city][:lon]