【发布时间】:2013-05-13 11:52:50
【问题描述】:
我有一个 JSON 文件,其中包含一组画廊,每个画廊都有自己的照片数组:
[
{
"title":"Some Title",
"photographs":[
{
"title": "Boat Ramp"
},
{
"title": "Security Camera"
},
{
"title": "Exhaust Vents"
},
{
"title": "Factory 1"
},
{
"title": "Factory 2"
},
{
"title": "Exhaust Vents"
},
{
"title": "Viaduct"
},
{
"title": "Girders"
},
{
"title": "Office"
}
]
}
]
我正在使用以下方法将其解码为哈希:
galleries = ActiveSupport::JSON.decode(File.read('db/seed/galleries.json'))
我想获取一个包含文档中所有照片的数组。
这个文件的结构可能会改变,所以我想要一个搜索属性名称的答案,而不是它在哈希中的位置。
获取包含所有画廊中所有照片且不依赖于文档中照片位置的数组的最简单方法是什么?
【问题讨论】:
-
你的预期输出是什么?
-
@Priti 编辑了问题以使其更清晰。我希望将文档中的所有照片放在一个数组中。
-
I would like an answer that searches on the attribute name, not its location within the hash.是什么意思?需要明确的信息。 -
@Priti 不是使用它们在哈希中的位置来查找照片(IE:画廊>画廊>照片),我想要一个在节点内的数组中找到的任何对象的数组“照片”键。
标签: ruby-on-rails ruby json search hash