JavaScript返回[object,Object](JavaScript returning [object, Object])
我正在尝试使用jQuery从Youtube API解析一些JSON,这是我的代码到目前为止:
// these are defined in my real code var ajaxURL = URL + UserName + jsonFormat; var myObject = JSON.stringify(ajaxURL); $.getJSON(ajaxURL, function(data){ var htmlString = ""; $.each(data.data.items, function(i,item){ // Here's where we piece together the HTML htmlString += '<img src="'; htmlString += item.thumbnail; htmlString += '">'; }); // Pop our HTML in the #image DIV $('#image').html(htmlString); });JSON看起来像(这已被削减):
"data": { "updated": "2013-09-09T18:48:57.730Z", "totalItems": 1, "startIndex": 1, "itemsPerPage": 1, "items": [{ "id": "theID", "uploaded": "2013-09-05T13:48:53.000Z", "updated": "2013-09-05T13:49:23.000Z", "uploader": "username", "category": "People", "title": "the title", "description": "the description", "thumbnail": { "sqDefault": "https://i1.ytimg.com/vi/erferfr/default.jpg", "hqDefault": "https://i1.ytimg.com/vi/erferfr/hqdefault.jpg" },我试过.item.thumbnail[0]但这仍然无效。
任何帮助都非常感谢 - 对JSON来说还是新手!
I'm trying to use jQuery to parse some JSON from the Youtube API, here's my code so far:
// these are defined in my real code var ajaxURL = URL + UserName + jsonFormat; var myObject = JSON.stringify(ajaxURL); $.getJSON(ajaxURL, function(data){ var htmlString = ""; $.each(data.data.items, function(i,item){ // Here's where we piece together the HTML htmlString += '<img src="'; htmlString += item.thumbnail; htmlString += '">'; }); // Pop our HTML in the #image DIV $('#image').html(htmlString); });The JSON looks like(this has been cut down):
"data": { "updated": "2013-09-09T18:48:57.730Z", "totalItems": 1, "startIndex": 1, "itemsPerPage": 1, "items": [{ "id": "theID", "uploaded": "2013-09-05T13:48:53.000Z", "updated": "2013-09-05T13:49:23.000Z", "uploader": "username", "category": "People", "title": "the title", "description": "the description", "thumbnail": { "sqDefault": "https://i1.ytimg.com/vi/erferfr/default.jpg", "hqDefault": "https://i1.ytimg.com/vi/erferfr/hqdefault.jpg" },I have tried .item.thumbnail[0] but this still doesn't work.
Any help is much appreciated - rather new to JSON!
最满意答案
您需要通过item.thumbnail.sqDefault和item.thumbnail.hqDefault访问您的数据,因为它是一个名为props的对象。
You need to access your data via item.thumbnail.sqDefault and item.thumbnail.hqDefault since it's an object w/ named props.
更多推荐
发布评论