具有 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']
如果您需要更多帮助,请更具体。
所以我对点击和解析 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']
如果您需要更多帮助,请更具体。