I'm working on a Ruby code that receive a json file (from Flurry API) and create another json friendly-format for "Status Board" app. When using this json fragment:
"country":[{"@country":"US","day":[{"@date":"2013-09-20","@value":"1"},
{"@date":"2013-09- 23","@value":"1"}]},
{"@country":"KR","day":{"@date":"2013-09-20","@value":"1"}}
Everything works fine, but when the code read "KR" and then "day" (note the lack of "[]") I receive the following error:
- `[]': can't convert String into Integer (TypeError)
The code I use to read the original json file is:
countries = response.parsed_response["country"]
countries.each do |datapoint|
countryName = datapoint["@country"]
days = datapoint["day"]
data = []
days.each do |datapoint2|
value = datapoint2["@value"]
date = Date.parse(datapoint2["@date"])
dateString = date.strftime(dateFormat)
data << { :title => dateString, :value => value }
end
dataSequences << { :title => countryName, :color => metric[:color], :datapoints => data
}
end
I'm kind of noob in Ruby, so...It is possible to add brackets when reading a json file to avoid this error?