0

我正在使用 Jade 模板处理 Harp 项目。我已经_data.json填充了内容,但我无法以我认为可以的方式访问它。鉴于此 JSON 结构:

{
"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(“外部照片”)。尝试mountain-residence.sections['exterior'].title也不行。我可以使用 访问相同的属性mountain-residence.sections[0].title,并且我的 JSON 似乎是有效的,但我需要用它的名称调用对象......有没有办法做到这一点?

例如,"mountain-residence"填充一个页面,我的总体想法是让每个对象"sections"填充它自己的子页面,这就是为什么我希望能够通过我的 JSON 按名称调用它。

4

1 回答 1

0

如评论中所述,sections 是一个数组,因此只有索引表示法有效。如果您想通过部分的名称来获取它,它需要是本机对象,或者通过您编写的预处理代码转换为对象。

于 2014-08-10T23:38:48.793 回答