第一次在這裏發佈。 無法進入JSON,我可以使用一些活動的。通過Ruby中嵌套的JSON迭代
,我需要的數據是在這個級別:
restaurant["menu"][0]["children"][0]["name"]
restaurant["menu"][0]["children"][0]["id"]
我想基於「名」的「ID」 S數組。
這是我的工作方法:
def find_burgers(rest)
array = []
rest["menu"].each do |section|
section["children"].each do |innersection|
innersection["name"].downcase.split.include?("burger")
array.push(innersection["id"])
end
end
return array
end
正如你可以想像,我又回到每一個「ID」的漢堡包數組,而不僅僅是「ID」 S。我已經嘗試了許多.map和.keep_if的組合。
感謝您的閱讀。
編輯:這是一個菜單項:
{
"children" => [
[ 0] {
"availability" => [
[0] 0
],
"children" => [
[0] {
"children" => [
[0] {
"availability" => [
[0] 0
],
"descrip" => "",
"id" => "50559491",
"is_orderable" => "1",
"name" => "Single",
"price" => "0.00"
},
[1] {
"availability" => [
[0] 0
],
"descrip" => "",
"id" => "50559492",
"is_orderable" => "1",
"name" => "Double",
"price" => "2.25"
}
],
"descrip" => "What Size Would You Like?",
"free_child_select" => "0",
"id" => "50559490",
"is_orderable" => "0",
"max_child_select" => "1",
"max_free_child_select" => "0",
"min_child_select" => "1",
"name" => "Milk Burger Size"
},
[1] {
"children" => [
[0] {
"availability" => [
[0] 0
],
"descrip" => "",
"id" => "50559494",
"is_orderable" => "1",
"name" => "Bacon",
"price" => "2.00"
}
],
"descrip" => "Add",
"free_child_select" => "0",
"id" => "50559493",
"is_orderable" => "0",
"max_child_select" => "1",
"max_free_child_select" => "0",
"min_child_select" => "0",
"name" => "Burgr Ad Bacon Optn"
}
],
"descrip" => "American cheese, lettuce, tomato and Milk Sauce",
"id" => "50559489",
"is_orderable" => "1",
"name" => "Milk Burger",
"price" => "4.25"
},
你能也張貼JSON /解析的哈希? – August 2014-11-21 03:39:20
我會在上面貼一張。 – 2014-11-21 04:22:07