Ruby on rails 在Ruby中迭代嵌套JSON
第一次在这里发布。 在输入JSON时遇到问题,我可以使用一些活动的 我需要的数据是这个级别的:Ruby on rails 在Ruby中迭代嵌套JSON,ruby-on-rails,ruby,json,Ruby On Rails,Ruby,Json,第一次在这里发布。 在输入JSON时遇到问题,我可以使用一些活动的 我需要的数据是这个级别的: restaurant["menu"][0]["children"][0]["name"] restaurant["menu"][0]["children"][0]["id"] 我想要一个基于名称的ID数组 这是我正在使用的方法: def find_burgers(rest) array = [] rest["menu"].each do |section| secti
restaurant["menu"][0]["children"][0]["name"]
restaurant["menu"][0]["children"][0]["id"]
我想要一个基于名称的ID数组
这是我正在使用的方法:
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。我尝试了许多.map和.keep_的组合
谢谢你的阅读
编辑:这是一个菜单项:
{
"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"
},
通常,您可以像这样迭代嵌套哈希:
def iterate(h)
h.each do |k, v|
if v.is_a?(Hash) || v.is_a?(Array)
iterate(v)
else
puts("k is #{k}, value is #{v}")
end
end
end
但是,由于您有具体的、硬编码的名称、子项、名称等,因此似乎只有这样做了。通常,您可以通过如下方式迭代嵌套哈希:
def iterate(h)
h.each do |k, v|
if v.is_a?(Hash) || v.is_a?(Array)
iterate(v)
else
puts("k is #{k}, value is #{v}")
end
end
end
但是,既然你有具体的、硬编码的名字,比如孩子、名字等等,那么似乎唯一的办法就是你这样做。你在做一个测试,看看这个名字是否包含汉堡,但你没有对测试结果做任何事。请尝试以下方法:
def find_burgers(rest)
array = []
rest["menu"].each do |section|
section["children"].each do |innersection|
array.push(innersection["id"]) if innersection["name"].downcase.split.include?("burger")
end
end
return array
end
也可以考虑使用正则表达式而不是“DeasCase.Seal.Cube”,像这样:
def find_burgers(rest)
array = []
rest["menu"].each do |section|
section["children"].each do |innersection|
array.push(innersection["id"]) if innersection["name"] =~ /\bburger\b/i
end
end
return array
end
如果名称包含由分词包围的字符串burger\b忽略大小写/i,则正则表达式返回true
最后,我认为您可以使用更实用的方法,如:
def find_burgers(rest)
rest["menu"].map do |section|
section["children"].select do |innersection|
innersection["name"] =~ /\bburger\b/i
end
end.flatten.map {|item| item["id"] }
end
select只返回那些与正则表达式匹配的项,第一个映射为每个节传回一个匹配的innersection数组,展平将数组数组转换为一个简单数组,最后一个映射仅从每个innersection中提取id
我认为我做得太过分了。您正在执行一个测试,以查看名称是否包含汉堡,但您没有对测试结果进行任何处理。请尝试以下方法:
def find_burgers(rest)
array = []
rest["menu"].each do |section|
section["children"].each do |innersection|
array.push(innersection["id"]) if innersection["name"].downcase.split.include?("burger")
end
end
return array
end
也可以考虑使用正则表达式而不是“DeasCase.Seal.Cube”,像这样:
def find_burgers(rest)
array = []
rest["menu"].each do |section|
section["children"].each do |innersection|
array.push(innersection["id"]) if innersection["name"] =~ /\bburger\b/i
end
end
return array
end
如果名称包含由分词包围的字符串burger\b忽略大小写/i,则正则表达式返回true
最后,我认为您可以使用更实用的方法,如:
def find_burgers(rest)
rest["menu"].map do |section|
section["children"].select do |innersection|
innersection["name"] =~ /\bburger\b/i
end
end.flatten.map {|item| item["id"] }
end
select只返回那些与正则表达式匹配的项,第一个映射为每个节传回一个匹配的innersection数组,展平将数组数组转换为一个简单数组,最后一个映射仅从每个innersection中提取id
我觉得我做得太过分了。一个更准确的答案,在JSON中迭代哈希或数组
j = {'key$1' => 'asdada',
'key$2' => ['key$3' => 2,
'key$4' => 's',
'key$6' => ['key$7' => 'man',
'key$8' => 'super']
],
'key5' => 5 }
def iterate(i)
if i.is_a?(Hash)
i.each do |k, v|
if v.is_a?(Hash) || v.is_a?(Array)
puts("k is #{k}, value is #{v}")
iterate(v)
else
puts("k is #{k}, value is #{v}")
end
end
end
if i.is_a?(Array)
i.each do |v|
iterate(v)
end
end
end
iterate(j)
在JSON中迭代哈希或数组的更准确的答案
j = {'key$1' => 'asdada',
'key$2' => ['key$3' => 2,
'key$4' => 's',
'key$6' => ['key$7' => 'man',
'key$8' => 'super']
],
'key5' => 5 }
def iterate(i)
if i.is_a?(Hash)
i.each do |k, v|
if v.is_a?(Hash) || v.is_a?(Array)
puts("k is #{k}, value is #{v}")
iterate(v)
else
puts("k is #{k}, value is #{v}")
end
end
end
if i.is_a?(Array)
i.each do |v|
iterate(v)
end
end
end
iterate(j)
你也可以发布JSON/解析哈希吗?我会在上面发布一个。你也可以发布JSON/解析哈希吗?我会在上面发布一个。就这样!我刚开始写代码,我浪费了多少时间?没时间了,所以我真的很感激。酷!坚持下去——这会变得更容易。如果你需要的话,请考虑接受我的回答。我刚开始写代码,我浪费了多少时间?没时间了,所以我真的很感激。酷!坚持下去——这会变得更容易。如果你需要的话,请考虑接受我的回答。