Как обработать hash из json?
Всем привет.
Пытаюсь разобраться с простыми конструкциями руби. Получаю массив из json данных:
response = JSON.parse(RESPONSE)
Пытаюсь его обработать таким образом:
class GetFilm
attr_accessor :group_theaters :parameter
def initialize(*catalogue)
title = self.get_data(catalogue, "film")
@group_theaters = title
end
def get_data(*catalogue, parameter)
@array = catalogue.length
end
end
Отправляю данные:
s = GetFilm.new(response.values.to_a)
puts s.group_theaters
Почему-то catalogue.length
у меня всегда 1, изначально json имеет вид:
'{"catalog": {
"1": {
"title": "Left Behind",
"theaters": ["Ukraine", "Big World"],
},
"2": {
"title": "Into the storm",
"theaters": ["Ukraine", "Big World"],
}
}}'
Обновление
RESPONSE1 = '{"catalog": {
"1": {
"title": "Left Behind",
"theaters": ["Ukraine", "Big World"]
},
"2": {
"title": "Into the storm",
"theaters": ["Ukraine", "Big World"]
} }}'
response = JSON.parse(RESPONSE1)
s = GetFilm.new(response.values)
И то, что получилось: 1
puts catalogue
в методе get_data
показывает массив в фигурных скобках:
{"1"=>{"title"=>"Left Behind", "theaters"=>["Ukraine", "Big World"]}, "2"=>{"title"=>"Into the storm", "theaters"=>["Ukraine", "Big World"]}}
Источник: Stack Overflow на русском