Thursday, 22 August 2013

HTTPArty/Rails: How to handle the Response array

HTTPArty/Rails: How to handle the Response array

I am calling an API that returns an array of JSON objects and I cant seem
to properly access each element. This is a sample return value of the API
call
[{"param1":1,"param2":"blah1"},
{"param1":2,"param2":"blah2"},
{"param1":3,"param2":"blah3"}]
Here is me trying to call the API and print the first JSON response
object's parameter
result = HTTParty.get('http://randomapi.com',query: options)
@a = result[0]['param1']
# puts "#{result}"
puts "#{@a}"
Doing this prints nothing. I know I am successfully accessing the URL
because result will print the correct information.
I got information on how to access the JSON response through this URL
http://blog.teamtreehouse.com/its-time-to-httparty

No comments:

Post a Comment