Youtube 视频时长 API v3
问题描述:
大家好,我对 youtube 的 api v3 有一个小问题.
Hey guys i have a little problem with the api v3 of youtube.
问题是,如果json有这个结构
Question is, if the json have this structure
{
"kind": "youtube#videoListResponse",
"etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/1jshwBhrF6O49W3oUmNF7rmkgAQ\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#video",
"etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/Zji9LPuiDCpb4GjgPpzdGgf85ws\"",
"id": "3wFq1-pK05o",
"contentDetails": {
"duration": "PT1M1S",
"dimension": "2d",
"definition": "hd",
"caption": "true",
"licensedContent": true
}
}
]
}
我想要像这样获得视频时长?
$.getJSON( "https://www.googleapis.com/youtube/v3/videos?id=XXXX&key=XXXXX&part=contentDetails", function( json ) {
document.write( "JSON Data: " + json["items"]["contentDetails"]["duration"] );
});
但它不起作用!我不明白出了什么问题.在我发疯之前请帮助我.
But it doesn't work! I do not understand what's wrong. Please help me before I go crazy.
答
试试这个:
var json = {
"kind": "youtube#videoListResponse",
"etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/1jshwBhrF6O49W3oUmNF7rmkgAQ\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [{
"kind": "youtube#video",
"etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/Zji9LPuiDCpb4GjgPpzdGgf85ws\"",
"id": "3wFq1-pK05o",
"contentDetails": {
"duration": "PT1M1S",
"dimension": "2d",
"definition": "hd",
"caption": "true",
"licensedContent": true
}
}]
}
console.log(json.items[0].contentDetails.duration);