简体   繁体   English

使用Jade和Express访问对象内的json对象

[英]Accessing json objects within objects with Jade and express

I'm pulling my hair out a bit at the moment. 我现在正在拔头发。 I can't seem to figure out how to access the "media" content within the following json object using Jade. 我似乎无法弄清楚如何使用Jade访问以下json对象中的“媒体”内容。

   {
      "summary":"Jose Mourinho names his Real Madrid side to face Borussia Dortmund in the Champions League semi-final 24 hours early.",
      "type":"STY",
      "lastUpdated":"2013-04-23T16:31:39+00:00",
      "firstCreated":"2013-04-23T16:31:39+00:00",
      "hasShortForm":true,
      "media":{
         "images":{
            "index":{
               "67193384":{
                  "height":261,
                  "width":464,
                  "href":"http://thesun.co.uk/media/images/67193000/jpg/_67193384_67193383.jpg",
                  "altText":"Jose Mourinho"
               }
            }
         }
      },
   },

I can access summary, type, updated etc. But i cannot figure out how to access the image meta data within media.images.index.67193384 我可以访问摘要,类型,更新等。但是我不知道如何访问media.images.index.67193384中的图像元数据。

for item in results
    p #{item.summary}
    p #{item.lastUpdated}
    p #{item.media[0]} // ???

Can someone please help me figure out? 有人可以帮我弄清楚吗? I've never tried to access data that's an object within an object within an object. 我从未尝试访问对象内对象中对象内的数据。 Also, the 67193384 object within images.index is unique and will always be different from result to result. 另外, images.index中的67193384对象是唯一的,并且每个结果之间始终是不同的

Thanks! 谢谢!

Bit of a hack, but it works: 有点hack,但是可以用:

- if (item.media && item.media.images)
  p #{item.media.images.index[Object.keys(item.media.images.index)[0]].height}
for item in results
    p= item.summary
    p= item.lastUpdated
    - for (var key in item.media.images) {break;}
    p= item.images.index[key].height

The for loop is used to get your key . for循环用于获取密钥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM