簡體   English   中英

使用 HTTParty 和解析/訪問響應的菜鳥

[英]Noob with HTTParty and parsing / accessing responses

所以我對命中和解析 API 的世界是全新的,使用郵遞員,我已經能夠命中 api 並得到響應,其中包含我需要的信息。

這是作為響應返回的內容。

=> #<HTTParty::Response:0x1e2a8 parsed_response={"success"=>true, "error"=>nil, "response"=>[{"loc"=>{"lat"=>51.062831878662, "long"=>-113.88871002197}, "place"=>{"name"=>"chestermere", "state"=>"ab", "country"=>"ca"}, "periods"=>[{"timestamp"=>1600819200, "dateTimeISO"=>"2020-09-22T18:00:00-06:00", "tempC"=>15.14, "tempF"=>59.26, "feelslikeC"=>15.14, "feelslikeF"=>59.26, "dewpointC"=>3.63, "dewpointF"=>38.53, "humidity"=>44, "pressureMB"=>1010.6, "pressureIN"=>29.84, "windDir"=>"SSE", "windDirDEG"=>158, "windSpeedKTS"=>4.86, "windSpeedKPH"=>9, "windSpeedMPH"=>5.59, "windGustKTS"=>10, "windGustKPH"=>18.52, "windGustMPH"=>11.51, "precipMM"=>0, "precipIN"=>0, "snowCM"=>0, "snowIN"=>0, "visibilityKM"=>24.135, "visibilityMI"=>14.997, "sky"=>69, "cloudsCoded"=>"SC", "weather"=>"Partly Cloudy", "weatherCoded"=>"::SC", "weatherPrimary"=>"Partly Cloudy", "weatherPrimaryCoded"=>"::SC", "icon"=>"pcloudy.png", "solradWM2"=>204, "uvi"=>0, "isDay"=>true}, {"timestamp"=>1600822800, "dateTimeISO"=>"2020-09-22T19:00:00-06:00", "tempC"=>14.46, "tempF"=>58.02, "feelslikeC"=>14.46, "feelslikeF"=>58.02, "dewpointC"=>4.13, "dewpointF"=>39.44, "humidity"=>49, "pressureMB"=>1011.8, "pressureIN"=>29.88, "windDir"=>"SSE", "windDirDEG"=>157, "windSpeedKTS"=>5.74, "windSpeedKPH"=>10.63, "windSpeedMPH"=>6.61, "windGustKTS"=>9, "windGustKPH"=>16.67, "windGustMPH"=>10.36, "precipMM"=>0, "precipIN"=>0, "snowCM"=>0, "snowIN"=>0, "visibilityKM"=>24.135, "visibilityMI"=>14.997, "sky"=>44, "cloudsCoded"=>"SC", "weather"=>"Partly Cloudy", "weatherCoded"=>"::SC", "weatherPrimary"=>"Partly Cloudy", "weatherPrimaryCoded"=>"::SC", "icon"=>"pcloudy.png", "solradWM2"=>47, "uvi"=>0, "isDay"=>true}], "profile"=>{"tz"=>"America/Edmonton", "tzname"=>"MDT", "tzoffset"=>-21600, "isDST"=>true, "elevFT"=>nil, "elevM"=>nil}}]}, @response=#<Net::HTTPOK 200 OK readbody=true>, @headers={"content-type"=>["application/json; charset=utf-8"], "transfer-encoding"=>["chunked"], "connection"=>["close"], "date"=>["Wed, 23 Sep 2020 00:44:11 GMT"], "server"=>["nginx/1.17.10"], "pragma"=>["public"], "x-aeris-auto"=>["0"], "x-cacheaction-stored"=>["action__conditions_index_52ac6d0b54d5aa23bbb2c41fa8c79bbf ttl 60"], "expires"=>["Wed, 23 Sep 2020 00:45:11 GMT"], "cache-control"=>["public,max-age=60,s-maxage=60"], "access-control-allow-origin"=>["*"], "access-control-allow-methods"=>["GET, POST, OPTIONS"], "access-control-allow-headers"=>["DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range"], "access-control-expose-headers"=>["Content-Length,Content-Range"], "x-cache"=>["Miss from cloudfront"], "via"=>["1.1 2241406ac19fffc8f35d6ddef8e22f56.cloudfront.net (CloudFront)"], "x-amz-cf-pop"=>["SEA19-C1"], "x-amz-cf-id"=>["pnWOSthyXcky5YEtPxN34T5ViEStc8PPNJUasdvHemgUS7f6y7T5hg=="]}>

現在我需要能夠在“響應”=>[{...}] 中訪問和保存或顯示數據,這就是讓我感到困惑的原因。 我不確定如何在各個 ""=> {} 部分中調用數據?

這里的任何幫助都會很棒,不太確定去哪里找到我正在尋找的文檔。 當我做類似 JSON.parse(response.body) 的事情時,它給了我這個:

=> {"success"=>true, "error"=>nil, "response"=>[{"loc"=>{"lat"=>51.062831878662, "long"=>-113.88871002197}, "place"=>{"name"=>"chestermere", "state"=>"ab", "country"=>"ca"}, "periods"=>[{"timestamp"=>1600819200, "dateTimeISO"=>"2020-09-22T18:00:00-06:00", "tempC"=>15.14, "tempF"=>59.26, "feelslikeC"=>15.14, "feelslikeF"=>59.26, "dewpointC"=>3.63, "dewpointF"=>38.53, "humidity"=>44, "pressureMB"=>1010.6, "pressureIN"=>29.84, "windDir"=>"SSE", "windDirDEG"=>158, "windSpeedKTS"=>4.86, "windSpeedKPH"=>9, "windSpeedMPH"=>5.59, "windGustKTS"=>10, "windGustKPH"=>18.52, "windGustMPH"=>11.51, "precipMM"=>0, "precipIN"=>0, "snowCM"=>0, "snowIN"=>0, "visibilityKM"=>24.135, "visibilityMI"=>14.997, "sky"=>69, "cloudsCoded"=>"SC", "weather"=>"Partly Cloudy", "weatherCoded"=>"::SC", "weatherPrimary"=>"Partly Cloudy", "weatherPrimaryCoded"=>"::SC", "icon"=>"pcloudy.png", "solradWM2"=>204, "uvi"=>0, "isDay"=>true}, {"timestamp"=>1600822800, "dateTimeISO"=>"2020-09-22T19:00:00-06:00", "tempC"=>14.46, "tempF"=>58.02, "feelslikeC"=>14.46, "feelslikeF"=>58.02, "dewpointC"=>4.13, "dewpointF"=>39.44, "humidity"=>49, "pressureMB"=>1011.8, "pressureIN"=>29.88, "windDir"=>"SSE", "windDirDEG"=>157, "windSpeedKTS"=>5.74, "windSpeedKPH"=>10.63, "windSpeedMPH"=>6.61, "windGustKTS"=>9, "windGustKPH"=>16.67, "windGustMPH"=>10.36, "precipMM"=>0, "precipIN"=>0, "snowCM"=>0, "snowIN"=>0, "visibilityKM"=>24.135, "visibilityMI"=>14.997, "sky"=>44, "cloudsCoded"=>"SC", "weather"=>"Partly Cloudy", "weatherCoded"=>"::SC", "weatherPrimary"=>"Partly Cloudy", "weatherPrimaryCoded"=>"::SC", "icon"=>"pcloudy.png", "solradWM2"=>47, "uvi"=>0, "isDay"=>true}], "profile"=>{"tz"=>"America/Edmonton", "tzname"=>"MDT", "tzoffset"=>-21600, "isDST"=>true, "elevFT"=>nil, "elevM"=>nil}}]}

但我再次迷失了如何獲取其中的內容。

提前感謝你的幫助。 如果我缺乏,請不要猶豫,詢問更多信息!

只需將 json 放在一個變量中

example = {"success"=>true, "error"=>nil, "response"=>[{"loc"=>`{"lat"=>51.062831878662, "long" ... }` 

然后,例如你可以這樣做:

example['response'][0]['place']

如果您需要更多幫助,請更具體。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM