使用HTTParty的Noob和解析/访问响应

问题描述

因此,我对打入和解析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,"windDirDEG"=>157,"windSpeedKTS"=>5.74,"windSpeedKPH"=>10.63,"windSpeedMPH"=>6.61,"windGustKTS"=>9,"windGustKPH"=>16.67,"windGustMPH"=>10.36,"sky"=>44,"solradWM2"=>47,"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,"elevM"=>nil}}]}

但是我再次迷失了如何获取其中的内容

预先感谢您的协助。如果我缺少的话,请随时询问更多信息!

解决方法

只需将json放在变量中

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

然后,例如,您可以这样做:

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

如果您需要更多帮助,请更具体。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...