本文介绍了如何使用javascript API和json获取youtube播放列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的YouTube项目的一部分。我尝试从JSON格式中提取视频信息,但我在这一行有问题:
This is part of my youtube project. I try to extract video information from JSON format but I have problem in this line:
var videoId = data.feed.entry[i].link[1].href;
当我在单行中执行此操作时,不在cikle evrithing是可以的但是在cikle for或者我有错误。
When I do this in single line not in cikle evrithing is ok but in cikle for or while I have error.
//get youtube ID
function extract(url){
pos1=url.indexOf("videos/");
pos2=url.substr(42,11);
return pos2;
}
//My playlist LINK
var url2="http://gdata.youtube.com/feeds/api/playlists/B2A4E1367126848D?v=2&alt=json";
function playlistinfo(url1) {
$.ajax({
url: url1,
dataType: "jsonp",
success: function (data) { parseresults(data); }
});
}
//whit this get playlist data
function parseresults(data) {
//return playlist clip number
var klipove= data.feed.openSearch$totalResults.$t;
//put clips in <li>
for(i=0;i<=klipove-1;i++) {
var videoId = data.feed.entry[i].link[1].href;
//get video id ID
var id= extract(videoId);
thumb = data.feed.entry[i].media$group.media$thumbnail[0].url;
$('<li><img src="'+thumb+'" alt="'+id+'" class="thumb"/></li>').appendTo('.cont');
}
}
推荐答案
恕我直言如果您使用 $。getJSON
, $。每个
,您的代码可以大大缩短试试这个。
IMHO, you code can be much shortened if you use $.getJSON
, $.each
Try this.
var playListURL = 'http://gdata.youtube.com/feeds/api/playlists/B2A4E1367126848D?v=2&alt=json&callback=?';
var videoURL= 'http://www.youtube.com/watch?v=';
$.getJSON(playListURL, function(data) {
var list_data="";
$.each(data.feed.entry, function(i, item) {
var feedTitle = item.title.$t;
var feedURL = item.link[1].href;
var fragments = feedURL.split("/");
var videoID = fragments[fragments.length - 2];
var url = videoURL + videoID;
var thumb = "http://img.youtube.com/vi/"+ videoID +"/default.jpg";
if (videoID !='videos') {
list_data += '<li><a href="'+ url +'" title="'+ feedTitle +'"><img alt="'+ feedTitle+'" src="'+ thumb +'"</a></li>';
}
});
$(list_data).appendTo(".cont");
});
演示:
PS:请记住,youtube视频的缩略图可以在
P.S: Keep in mind that thumbnail for a youtube video could be found at
http://img.youtube.com/vi/{video-id}/default.jpg
()
这篇关于如何使用javascript API和json获取youtube播放列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!