【问题标题】:JavaScript returning [object, Object]JavaScript 返回 [object, Object]
【发布时间】:2013-09-13 08:28:38
【问题描述】:

我正在尝试使用 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 来说相当新!

【问题讨论】:

  • 你试过item.thumbnail.sqDefault 吗?
  • 试试item.thumbnail["sqDefault"]
  • @SB - 完美 - 请将此作为答案,以便我接受。以为我是对的!

标签: jquery json youtube-api


【解决方案1】:

您需要通过item.thumbnail.sqDefaultitem.thumbnail.hqDefault 访问您的数据,因为它是一个带有命名道具的对象。

【讨论】:

    猜你喜欢
    • 2015-05-09
    • 2020-07-07
    • 1970-01-01
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2015-07-16
    相关资源
    最近更新 更多