我正在尝试将YouTube Data API 3的响应解析为我的node.js项目。当我调用JSON.parse()时,它会在JSON中间停止并抛出一个"意外结束的JSON输入"错误。
我对node.js和YouTube Data API有了新的认识,但我无法找到任何可能导致此错误的JSON问题。
我的要求代码:
https.get('https://www.googleapis.com/youtube/v3/search?part=snippet&q=' + gameSearch + '+trailer&type=video&videoCaption=closedCaption&key=Google_API_Key', (res) => {
res.on('data', (d) => {
var result = JSON.parse(d.toString('utf8');
console.log(result);
响应数据(仅针对上下文的示例):
{
"kind": "youtube#searchListResponse",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/c5qJp8_01Z7pygk7E4FxXPySdrE\"",
"nextPageToken": "CAUQAA",
"regionCode": "US",
"pageInfo": {
"totalResults": 834115,
"resultsPerPage": 5
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/Kd8M7QMYXGvUuq22Gmm8tSsH2Og\"",
"id": {
"kind": "youtube#video",
"videoId": "QpvM9uwOcUc"
},
"snippet": {
"publishedAt": "2011-06-09T13:13:41.000Z",
"channelId": "UC64oAui-2WN5vXC7hTKoLbg",
"title": "Elder Scrolls 5 : Skyrim Official Trailer",
"description": "Elder Scrolls 5 Skyrim - E3 2011 Trailer. Join us on Facebook : http://facebook.com/GameNewsOfficial. Subscribe Now ! Bethesda ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/QpvM9uwOcUc/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/QpvM9uwOcUc/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/QpvM9uwOcUc/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "GameNewsOfficial",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/9ufokW0y_tAzTii-9cOHe9z-V9M\"",
"id": {
"kind": "youtube#video",
"videoId": "JSRtYpNRoN0"
},
"snippet": {
"publishedAt": "2011-02-23T20:12:28.000Z",
"channelId": "UCvZHe-SP3xC7DdOk4Ri8QBw",
"title": "The Elder Scrolls V: Skyrim - Official Trailer",
"description": "We're pleased to share the first ever in-game trailer for The Elder Scrolls V: Skyrim, the next installment in The Elder Scrolls series from award-winning creators ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/JSRtYpNRoN0/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/JSRtYpNRoN0/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/JSRtYpNRoN0/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Bethesda Softworks",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/JmkNelVuuT7Bu7O7dcOyMIu-rIw\"",
"id": {
"kind": "youtube#video",
"videoId": "yoXFk-0NrDI"
},
"snippet": {
"publishedAt": "2010-12-13T21:36:13.000Z",
"channelId": "UCvZHe-SP3xC7DdOk4Ri8QBw",
"title": "The Elder Scrolls V: Skyrim Announcement Trailer",
"description": "Watch the announcement trailer for The Elder Scrolls V: Skyrim... coming to Xbox 360, PlayStation 3, and PC on 11.11.11 ESRB RATING: MATURE with Blood ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/yoXFk-0NrDI/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/yoXFk-0NrDI/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/yoXFk-0NrDI/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Bethesda Softworks",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/BNQ1UBBPS3Zb9cjx8Mu1yDEFlo0\"",
"id": {
"kind": "youtube#video",
"videoId": "iX1JmaYdWa0"
},
"snippet": {
"publishedAt": "2014-04-26T02:29:59.000Z",
"channelId": "UCY7SikBhJcjMbMaN_L7l3WQ",
"title": "Skyrim - Wyrmstooth trailer",
"description": "SKYRIM WYRMSTOOTH : http://www.nexusmods.com/skyrim/mods/25704/? ENB : Vividian - http://www.nexusmods.com/skyrim/mods/36067/? MUSIC : 00:00 ...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/iX1JmaYdWa0/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/iX1JmaYdWa0/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/iX1JmaYdWa0/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Tyroine",
"liveBroadcastContent": "none"
}
},
{
"kind": "youtube#searchResult",
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/C80AcqmGPGxNkgpTifXvbWmFFBs\"",
"id": {
"kind": "youtube#video",
"videoId": "aLt0rSKClLA"
},
"snippet": {
"publishedAt": "2011-03-04T20:50:59.000Z",
"channelId": "UCU25Nxe7qWJKtNaTqU9GdSQ",
"title": "TES:V Skyrim Official Trailer HD (české titulky)",
"description": "",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/aLt0rSKClLA/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/aLt0rSKClLA/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/aLt0rSKClLA/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Kamil Doskočil",
"liveBroadcastContent": "none"
}
}
]
}
我一直收到错误:
undefined:46
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/9ufokW0y_tAzTii-9cOHe9z-V9M\"
SyntaxError: Unexpected end of JSON input
at Object.parse (native)
at IncomingMessage.https.get.res.on (/home/ubuntu/workspace/oldFile.js:289:23)
at emitOne (events.js:96:13)
at readableAddChunk (_stream_readable.js:176:18)
at IncomingMessage.Readable.push (_stream_readable.js:134:10)
at HTTPParser.parserOnBody (_http_common.js:129:22)
at TLSSocket.socketOnData (_http_client.js:361:20)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
我可以看到它似乎正在解析直到" items"中的第二个元素。阵列,我无法弄清楚它为什么会破碎!!
干杯
答案 0 :(得分:1)
在响应结束之前,您需要保留所有正在接收的数据块:
https.get('https://www.googleapis.com/youtube/v3/search?part=snippet&q=' + gameSearch + '+trailer&type=video&videoCaption=closedCaption&key=Google_API_Key', (res) => {
const chunks = []
res.on('data', (d) => { chunks.push(d) })
res.on('end', () => {
var result = JSON.parse(Buffer.concat(chunks).toString())
console.log(result)
})
})