从数组中选择嵌套的JSON值

时间:2015-01-04 14:57:51

标签: javascript ajax json youtube getjson

考虑这个getJSON请求,该请求从youtube中为thebeatles提取频道详细信息:

var youtubeFEED = "https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername=thebeatles&key={{ google_api_key }}"

$.getJSON(youtubeFEED, function(data){
    console.log('data loaded');
});

这导致:

{
 "kind": "youtube#channelListResponse",
 "etag": "\"F9iA7pnxqNgrkOutjQAa9F2k8HY/8SEUwKelrqHehoM7_2t5MWZY3T8\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 5
 },
 "items": [
  {
   "kind": "youtube#channel",
   "etag": "\"F9iA7pnxqNgrkOutjQAa9F2k8HY/Il2dF5SRdky6_tqanN3hNuDyfxc\"",
   "id": "UC_x5XG1OV2P6uZZ5FSM9Ttw",
   "contentDetails": {
    "relatedPlaylists": {
     "uploads": "UU_x5XG1OV2P6uZZ5FSM9Ttw"
    },
    "googlePlusUserId": "111395306401981598462"
   }
  }
 ]
}

如何选择uploads(在这种情况下为UU_x5XG1OV2P6uZZ5FSM9Ttw)的值,并将其存储为变量?

举个例子,当我尝试:

console.log(data.items.contentDetails.relatedPlaylists.uploads);

我明白了:

Uncaught TypeError: Cannot read property 'relatedPlaylists' of undefined

奖励积分:我的目标实际上是为给定用户名生成所有上传的videoId的列表。没有上传播放列表ID,有没有办法做到这一点? This是我找到的唯一方法。

0 个答案:

没有答案