2015-06-20 99 views
1

我知道這是一個noob問題,但我是新來的JSON ...我不能訪問該對象的數據:無法訪問JSON對象的JavaScript

{ 
"kind": "youtube#channelListResponse", 
"etag": "\"Y3xTLFF3RLtHXX85JBgzzgp2Enw/7zZjjC0N0XTk8OrPCzfx2O9vPg8\"", 
"pageInfo": { 
    "totalResults": 1, 
    "resultsPerPage": 1 
}, 
"items": [ 
    { 
    "kind": "youtube#channel", 
    "etag": "\"Y3xTLFF3RLtHXX85JBgzzgp2Enw/_uCWU9q9VCvKwgXG_6vL636QCVU\"", 
    "id": "UCSWgmaFWOuYVWR8Z30n5qLQ", 
    "snippet": { 
    "title": "ChrisCodeX", 
    "description": "Channel Features: wateva\r\n-music\r\n-gaming\r\n-comedy\r\nSubscribe to stay tune!\r\n\r\nFun Fact, to the haters out there:\r\n\r\nBeing an xbox fan isn't wrong but I hope you're being sarcastic and you realize that's a myth evolved from peoples' insistance on proving they were getting their money's worth from XBL.\r\nThe \"online connection\" is determined solely by your personal internet speeds. In other words it has nothing to do with which console you play.", 
    "publishedAt": "2011-08-09T02:23:58.000Z", 
    "thumbnails": { 
    "default": { 
     "url": "https://yt3.ggpht.com/-UL6VyOBij08/AAAAAAAAAAI/AAAAAAAAAAA/Y4oSGlkvucw/s88-c-k-no/photo.jpg" 
    }, 
    "medium": { 
     "url": "https://yt3.ggpht.com/-UL6VyOBij08/AAAAAAAAAAI/AAAAAAAAAAA/Y4oSGlkvucw/s240-c-k-no/photo.jpg" 
    }, 
    "high": { 
     "url": "https://yt3.ggpht.com/-UL6VyOBij08/AAAAAAAAAAI/AAAAAAAAAAA/Y4oSGlkvucw/s240-c-k-no/photo.jpg" 
    } 
    }, 
    "localized": { 
    "title": "ChrisCodeX", 
    "description": "Channel Features: wateva\r\n-music\r\n-gaming\r\n-comedy\r\nSubscribe to stay tune!\r\n\r\nFun Fact, to the haters out there:\r\n\r\nBeing an xbox fan isn't wrong but I hope you're being sarcastic and you realize that's a myth evolved from peoples' insistance on proving they were getting their money's worth from XBL.\r\nThe \"online connection\" is determined solely by your personal internet speeds. In other words it has nothing to do with which console you play." 
    } 
    } 
    } 
] 
} 

我將如何得到縮略圖值默認網址?

我試着用

$-getJSON(url, function(data){ 
var url = data.items.snippet.thumbnails.default.url; 
}); 

但我得到的是錯誤類型錯誤:data.items [0] .snippet.thumbnail是未定義

+1

好,'thumbnail'確實是不確定的。縮略圖不是。您發佈的代碼和發佈的消息不匹配。 –

+0

@FelixKling當然,他們不匹配,因爲我沒有複製回覆,我輸入了它......但你明白我的問題,是嗎? –

回答

2

在對象的items屬性是一個數組,所以你在訪問時,它定義一個指數:

data.items[0].snippet.thumbnails.default.url 
+0

你怎麼知道什麼時候smth是一個數組或對象? –

+1

items屬性如下所示:''items「:[{」kind「:」youtube#channel「,...}]'。注意外面的[[}]'。所有其他屬性都只是用'{}'包裝的。 '{...}'表示一個對象,而'[{...}]'表示一個對象數組。在這種情況下,你只有一個項目。更多項目用逗號分隔:'[{...},{...}]'。 – redelschaap

+0

非常感謝:*下次我會記住我的想法:) –