如何在JSON中访问数组中的命名对象?
我正在使用Jade模板处理一个Harp项目。我在我的_data.json中填充了内容,我在以我认为可以的方式访问它时遇到了问题。鉴于此JSON结构:如何在JSON中访问数组中的命名对象?,json,pug,harp,Json,Pug,Harp,我正在使用Jade模板处理一个Harp项目。我在我的_data.json中填充了内容,我在以我认为可以的方式访问它时遇到了问题。鉴于此JSON结构: { "mountain-residence": { "slug": "mountain-residence", "title": "Mountain Residence", "lead": "A southeast facing home...", "thumb": "exterior/entry", "se
{
"mountain-residence": {
"slug": "mountain-residence",
"title": "Mountain Residence",
"lead": "A southeast facing home...",
"thumb": "exterior/entry",
"sections": [
{
"exterior": {
"slug": "exterior",
"title": "Exterior Photos",
"lead": "Embracing an entry...",
"thumb": "terrace",
"photos": [
{
"slug": "southeast",
"alt": "Mountain Room Overlook",
"caption": "Porch, deck and spa terrace"
},
{
"slug": "terrace",
"alt": "Southeast Terrace",
"caption": "Spa deck and landscape terrace"
},
{
"slug": "entry",
"alt": "Entry Courtyard",
"caption": "Entry court and pergola"
},
{
"slug": "porch",
"alt": "Entry Porch",
"caption": "Timber entry shelter"
}
]
}
},
{
"interior": {
"slug": "interior",
"title": "Interior Photos",
"lead": "The interior spaces...",
"thumb": "mountain-room2",
"photos": [
{
"slug": "mountain-room2",
"caption": "Entry opening to the Mountain Room"
},
{
"slug": "dining",
"caption": "Dining Room into Mountain Room"
},
{
"slug": "cellar1",
"caption": "Wine Cellar and Tasting Room"
},
{
"slug": "den",
"caption": "Den and Family Hearth"
}
]
}
},
{
"design-sketches": {
"slug": "design-sketches",
"title": "Design Sketches",
"lead": "A careful site...",
"thumb": "shower",
"photos": [
{
"slug": "schematic",
"caption": "Exterior Elevation Study"
},
{
"slug": "elevation",
"caption": "Elevation Color Studies"
},
{
"slug": "shower",
"caption": "Outdoor stone shower"
}
]
}
}
]
}
}
我不知道如何访问数组中的命名对象,如mountain-residence.sections.exterior.title exterior Photos。尝试山地住宅。部分['exterior']。标题也不起作用。我可以使用mountain residence.sections[0].title访问相同的属性,我的JSON似乎有效,但我需要使用其名称调用对象。。。有办法做到这一点吗
例如,mountain residence填充一个页面,我的总体想法是让节中的每个对象填充其自己的子页面,这就是为什么我希望能够通过我的JSON按名称调用它。如注释中所述,节是一个数组,因此只有索引符号才起作用。如果您想通过节的名称获取它,它需要是一个本地对象,或者通过预处理您编写的代码将其转换为一个对象 如果我们不知道您使用的是哪种语言,就无法真正帮助您。无论如何,我都怀疑mountain-residence.sections.exterior.title或mountain-residence.sections[0]。title是否有效。节是一个对象数组。您所能做的就是迭代数组,并将每个条目与您要查找的值进行比较。如果您使用的是JavaScript,请参阅您是否尝试过mountain residence.sections['exterior'].titleYeah,您将需要以某种方式迭代sections数组,但具体操作方式取决于您所使用的语言。如果没有这些信息,这个问题就毫无意义,还不如闭嘴。