0
그래서 여기에 내가하려는 일이 있습니다. Ruby 파일을 간단히 작성하면 사용자 입력란, 도시를 입력 한 다음 해당 도시의 날씨 결과를 반환합니다. 필자는 Ruby로 작성한 적이 없으며 API를 사용하지도 않았습니다. 그러나 여기에 나의 시도가있다. 아래JSON에서 특정 값 가져 오기
API 응답 :
> {"coord"=>{"lon"=>-85.68, "lat"=>40.11}, "weather"=>[{"id"=>501,
> "main"=>"Rain", "description"=>"moderate rain", "icon"=>"10d"}],
> "base"=>"stations", "main"=>{"temp"=>57.78, "pressure"=>1009,
> "humidity"=>100, "temp_min"=>57, "temp_max"=>60.01},
> "wind"=>{"speed"=>5.17, "deg"=>116.005}, "rain"=>{"1h"=>1.02},
> "clouds"=>{"all"=>92}, "dt"=>1475075671, "sys"=>{"type"=>3,
> "id"=>187822, "message"=>0.1645, "country"=>"US",
> "sunrise"=>1475062634, "sunset"=>1475105280}, "id"=>4917592,
> "name"=>"Anderson", "cod"=>200} [Finished in 2.0s]
루비 아래 파일 :
require 'net/http'
require 'json'
url = 'http://api.openweathermap.org/data/2.5/weather?q=anderson&APPID=5c89010425b4d730b7558f57234ea3c8&units=imperial'
uri = URI(url)
response = Net::HTTP.get(uri)
parsed = JSON.parse(response)
puts parsed #Print this so I can see results
inputs temp = JSON.parse(response)['main']['temp']
puts desc = JSON.parse(response)['weather']['description']
puts humid = JSON.parse(response)['main']['humidity']
puts wind = JSON.parse(response)['wind']['speed']
내가 단지 온도, 설명, 습도, 바람 등의 몇 가지 항목을 끌어 된 일을하려고했다 . 그러나 나는 그것을 올바르게 얻는 것처럼 보이지 않습니다. 나는 각 시도마다 정의되지 않은 오류가 계속 발생합니다. (보석이나 루비에 아직 내장되어 있지 않은 것을 사용하지 않고이 작업을 완료하고 싶습니다.) (아직 사용자 입력 용 부품을 작성하지 않았습니다)
완벽한 감사합니다! –
내 대답이 도움이 되었다면 그것을 수용 가능한 대답으로 표시 할 수 있습니까? 감사 :) – fylie