I'm using YQL to get a New York Times JSON feed. I tried to get a thumbnail by following this thread's approach: <img src="' + ( doc.multimedia[0].url || '' )+ '" /> but got ypeError: Cannot read property '0' of undefined. May I know how to get the first thumbnail for those that have the array?
"docs": [{
"headline": {
"main": "AP Source: Jackson to Be Introduced by Knicks",
"print_headline": "AP Source: Jackson to Be Introduced by Knicks"
}
}, {
"headline": {
"main": "Every Dog Has Its Data ",
"kicker": "Well",
"print_headline": "With Technology, Every Dog Has Its Data"
},
"multimedia": [{
"width": "190",
"url": "images/2014/03/11/science/11PETS1_SPAN/11PETS1-thumbWide.jpg",
"height": "126",
"subtype": "wide",
"legacy": {
"wide": "images/2014/03/11/science/11PETS1_SPAN/11PETS1-thumbWide.jpg",
"wideheight": "126",
"widewidth": "190"
},
"type": "image"
}, {
"width": "600",
"url": "images/2014/03/11/science/11PETS2/11PETS2-articleLarge.jpg",
"height": "338",
"subtype": "xlarge",
"legacy": {
"xlargewidth": "600",
"xlarge": "images/2014/03/11/science/11PETS2/11PETS2-articleLarge.jpg",
"xlargeheight": "338"
},
"type": "image"
}, {
"width": "75",
"url": "images/2014/03/11/science/11PETS3/11PETS3-thumbStandard.jpg",
"height": "75",
"subtype": "thumbnail",
"legacy": {
"thumbnailheight": "75",
"thumbnail": "images/2014/03/11/science/11PETS3/11PETS3-thumbStandard.jpg",
"thumbnailwidth": "75"
},
"type": "image"
}]
},
My code:
$(data.query.results.json.response.docs).each(function (index, doc) {
item_html += '<li>' + doc.headline.main + '<p><img src="' + (doc.multimedia[0].url || '') + '" /></li>';
});
doc.multimediacan beundefinedin that case.