这是身体:
"kind": "youtube#searchResult",
"etag": "\"RmznBCICv9YtgWaaa_nWDIH1_GM/uHwvNpWXbfDW_4Wj8a7WcvZxdKo\"",
"id": {
"kind": "youtube#video",
"videoId": "MXVWCuahiD4"
},
它是一个来自名为Camodo Gaming的频道的youtube api的输出,无论如何我需要获得说" videoID":" [id]"的部分。我不知道我应该如何分裂身体。谷歌无法回答这个问题。 这是我当前的代码(使用npm-module请求):
var request = require('request');
request('https://www.googleapis.com/youtube/v3/search?
part=snippet&channelId=UCC6ulNJ-KDoIa3VI2gcvQpA&maxResults=1&order=date&type=video&key={token}', function (error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
var str = body
//how can i split the string?
var split = str.split() // ?
console.log(split)
});
修改 这是整个身体:
{
"kind": "youtube#searchListResponse",
"etag": "\"RmznBCICv9YtgWaaa_nWDIH1_GM/-0EYfTuxeWYgCGHPclJlgnzp8FU\"",
"nextPageToken": "CAEQAA",
"regionCode": "FI",
"pageInfo": {
"totalResults": 858,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#searchResult",
"etag": "\"RmznBCICv9YtgWaaa_nWDIH1_GM/jlYGCUcC2rF87jrQ8VYhA_RRByk\"",
"id": {
"kind": "youtube#video",
"videoId": "paXZZap3bpo"
},
"snippet": {
"publishedAt": "2018-03-23T11:00:05.000Z",
"channelId": "UCC6ulNJ-KDoIa3VI2gcvQpA",
"title": "MASSIVE HURRICANE IN THE ZOMBIE APOCALYPSE! - GTA 5 Mod Gameplay - Zombie Multiplayer Roleplay",
"description": "MASSIVE HURRICANE IN THE ZOMBIE APOCALYPSE! - GTA 5 Mod Gameplay - Zombie Multiplayer Roleplay Welcome to Camodo Gaming's Let's Play of GTA 5 Zombie Apocalypse. We are trying out a zombie...",
"thumbnails": {
"default": {
"url": "https://i.ytimg.com/vi/paXZZap3bpo/default.jpg",
"width": 120,
"height": 90
},
"medium": {
"url": "https://i.ytimg.com/vi/paXZZap3bpo/mqdefault.jpg",
"width": 320,
"height": 180
},
"high": {
"url": "https://i.ytimg.com/vi/paXZZap3bpo/hqdefault.jpg",
"width": 480,
"height": 360
}
},
"channelTitle": "Camodo Gaming",
"liveBroadcastContent": "none"
}
}
]
}
答案 0 :(得分:0)
您的结果是JSON格式,可以很容易地解析为真正的JS对象,您可以从中调用.videoId
来获取videoId。
var request = require('request');
request('https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=UCC6ulNJ-KDoIa3VI2gcvQpA&maxResults=1&order=date&type=video&key={token}', function(error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
var json = JSON.parse(body);
console.log(json);
console.log(json.items[0].id.videoId);
});