Youtube API解析结果

时间:2015-06-02 13:46:19

标签: json youtube-api

我试图从JSON获取videoId值,JSON看起来像这样:

{
  "kind": "youtube#searchListResponse",
  "etag": "\"xmg9xJZuZD438sF4hb-VcBBREXc/NidRzvsME6YSWb6BE8c17XSdcIY\"",
  "nextPageToken": "CAUQAA",
  "pageInfo": {
    "totalResults": 145565,
    "resultsPerPage": 5
  },
  "items": [
    {
      "kind": "youtube#searchResult",
      "etag": "\"xmg9xJZuZD438sF4hb-VcBBREXc/hH0pUkaSBxdgtWXmrivFqgGx9dc\"",
      "id": {
        "kind": "youtube#video",
        "videoId": "QSwilU0kJF0"
      },
      "snippet": {
        "publishedAt": "2011-02-17T14:29:22.000Z",
        "channelId": "UCHWxEEGBQP67Gnx8K_T432A",
        "title": "David Guetta - The World Is Mine",
        "description": "",
        "thumbnails": {
          "default": {
            "url": "https://i.ytimg.com/vi/QSwilU0kJF0/default.jpg"
          },
          "medium": {
            "url": "https://i.ytimg.com/vi/QSwilU0kJF0/mqdefault.jpg"
          },
          "high": {
            "url": "https://i.ytimg.com/vi/QSwilU0kJF0/hqdefault.jpg"
          }
        },
        "channelTitle": "themoviebr",
        "liveBroadcastContent": "none"
      }
    },
    {
      "kind": "youtube#searchResult",
      "etag": "\"xmg9xJZuZD438sF4hb-VcBBREXc/5fjBFSKZ09TRgndWr-yUg9sXzmI\"",
      "id": {
        "kind": "youtube#video",
        "videoId": "WZJIKdCOh3o"
      },
      "snippet": {
        "publishedAt": "2009-09-15T00:16:07.000Z",
        "channelId": "UC2kTZB_yeYgdAg4wP2tEryA",
        "title": "David Guetta - The World Is Mine",
        "description": "David Guetta - The World Is Mine EMI France (P) 2004 Gum Records licence exclusive EMI Music France.",
        "thumbnails": {
          "default": {
            "url": "https://i.ytimg.com/vi/WZJIKdCOh3o/default.jpg"
          },
          "medium": {
            "url": "https://i.ytimg.com/vi/WZJIKdCOh3o/mqdefault.jpg"
          },
          "high": {
            "url": "https://i.ytimg.com/vi/WZJIKdCOh3o/hqdefault.jpg"
          }
        },
        "channelTitle": "emimusic",
        "liveBroadcastContent": "none"
      }
    },
    {
        "kind": "youtube#searchResult",
        "etag": "\"xmg9xJZuZD438sF4hb-VcBBREXc/0r8jweq5yQVWUv5rv8NSZKL2nuk\"",
        "id": {
          "kind": "youtube#video",
          "videoId": "3728hymqs4Q"
        },
        "snippet": {
          "publishedAt": "2011-01-31T00:49:45.000Z",
          "channelId": "UCEa-YyTVr_SSooVGBS9hkNg",
          "title": "David Guetta - Feat DJ Davis - The World Is Mine (HD)",
          "description": "",
          "thumbnails": {
            "default": {
              "url": "https://i.ytimg.com/vi/3728hymqs4Q/default.jpg"
            },
            "medium": {
              "url": "https://i.ytimg.com/vi/3728hymqs4Q/mqdefault.jpg"
            },
            "high": {
              "url": "https://i.ytimg.com/vi/3728hymqs4Q/hqdefault.jpg"
            }
          },
          "channelTitle": "ShyzerXtremeHardcore",
          "liveBroadcastContent": "none"
        }
      }
    ]
  }
}

我只需要第一个对象videoId,但我收到并且错误。以下是我获取id的函数的样子:

function showResponse(response) {
    var responseString = JSON.stringify(response);
    document.getElementById('response').innerHTML = responseString.items[0].id.videoId;
}

首先我将响应转换为JSON对象,然后我尝试获取videoId值,但我收到以下错误:无法读取属性' 0'未定义的。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您需要使用JSON.parse,而不是stringify:

function showResponse(response) {
    var responseString = JSON.parse(response);
    document.getElementById('response').innerHTML = responseString.items[0].id.videoId;
}

答案 1 :(得分:0)

请尝试以下javaScript

var responseString = JSON.parse(response);
id = responseString[items][0][id][videoId]