Getting rates from an array - json -
i trying rates website.
so connect website = faraday.get('https://bitpay.com/api/rates')).status == 200
, try parse this.
a segment of response is:
#<faraday::response:0x007fcf1ce25688 @env= #<struct faraday::env method=:get, body= "[{\"code\":\"btc\",\"name\":\"bitcoin\",\"rate\":1}, {\"code\":\"usd\",\"name\":\"us dollar\",\"rate\":586.66},{\"code\":\"eur\",\"name\":\"eurozone euro\",\"rate\":528.991322},{\"code\":\"gbp\",\"name\":\"pound sterling\",\"rate\":449.441986},{\"code\":\"jpy\",\"name\":\"japanese yen\",\"rate\":59907.95922},{\"code\":\"cad\",\"name\"
when website.body string class of these values found on website. want parse them though (json?) can each rate float.
i tried json.parse(website.body)["gbp"]["rate"].to_f
yet again cannot work in string.
the return typeerror: no implicit conversion of string integer
i having similar (but not same) format different rates website , how handling it. need change format first or there different way around it?
you're trying access parsed json key "gbp"
have array. it's if did
a = [1,2,3,4,5] a['foo']
try out
currencies = json.parse(website.body) currencies.each { |currency| puts currency['rate'] }
and change need
Comments
Post a Comment