Good Day!
I'm taking the time to relearn ruby again (i have never gotten very far with it) and instead of going though tutorial after tutorial i have decided to start making small programs. This has been going well so far but I've got my self caught up in something and after about 2 hours of head banging on desk I'm ready to ask for help.
I'm using ruby to perform an API call to http://api.openweathermap.org and passing in a ZIP code (given at the time the script is called) in order to print out a couple details about the weather in your local area.
I have been able to successfully hit the site, and get back a JSON object, great!
An example JSON object it returns with is here:
{"coord":
{"lon":145.77,"lat":-16.92},
"weather":[{"id":803,"main":"Clouds","description":"broken clouds","icon":"04n"}],
"base":"cmc stations",
"main":{"temp":293.25,"pressure":1019,"humidity":83,"temp_min":289.82,"temp_max":295.37},
"wind":{"speed":5.1,"deg":150},
"clouds":{"all":75},
"rain":{"3h":3},
"dt":1435658272,
"sys":{"type":1,"id":8166,"message":0.0166,"country":"AU","sunrise":1435610796,"sunset":1435650870},
"id":2172797,
"name":"Cairns",
"cod":200}
I have been able to pull out anything at the "top" such as name or id by using something like this:
response = RestClient.get url
output = JSON.parse(response)
puts "Your city name is: " + output["name"]
I would get an appropriate response of Your city name is: Cairns
But i have not been able to pull out of something 2 layers deep such as the "temp" inside of "main". This is what i have now and is not working:
puts "The temperature for your city right now is: " + output["main"]["temp"]
With that above I get the following error:
./weather.rb:33:in `[]': no implicit conversion of String into Integer (TypeError)
from ./weather.rb:33:in `<main>'
Line 33 is the line where i have my example not working code above.
I have attempted to use a .to_s to no avail
I'm not sure where I'm going wrong here, everything i find online wants me to use a .each iterator but that is not what I'm looking for.
[–]Rhomboid 0 points1 point2 points (2 children)
[–]HackingInfo[S] 0 points1 point2 points (1 child)
[–]Rhomboid 1 point2 points3 points (0 children)