简体   繁体   中英

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.

   {
      "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

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.

Thanks!

Bit of a hack, but it works:

- 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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