JSON - 从youtube播放列表中获取超过25个视频

时间:2012-03-01 17:37:54

标签: html json youtube

我有一个html页面,目前显示播放列表的视频,但它只返回前25个视频:

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>   
<script type="text/javascript" src="scripts/jquery.youtubepopup.js"></script> 
<script type='text/javascript'>//<![CDATA[
    $(window).load(function () {
        var playListURL = 'http://gdata.youtube.com/feeds/api/playlists/A6A0F817C1771FE5?v=2&alt=json&callback=?';
        var videoURL = 'http://www.youtube.com/watch?v=';
        $.getJSON(playListURL, function (data) {
            var list_data = "";
            var column_count = -1;
            var begin_table = "<table width='100%'><tr>";
            var end_table = "</table>";
            var html_data = "";
            $.each(data.feed.entry, function (i, item) {
                column_count = column_count + 1;
                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(column_count <= 3)
                    html_data += '<td><a class="youtube" href="#" rel="' + videoID + '" title="' + feedTitle + '"><img alt="' + feedTitle + '" src="' + thumb + '"></a></td>';
                else
                {
                    column_count = 0;
                    html_data += "</tr><tr>";
                    html_data += '<td><a class="youtube" href="#" rel="' + videoID + '" title="' + feedTitle + '"><img alt="' + feedTitle + '" src="' + thumb + '"></a></td>';
                }
            });
            html_data = begin_table + html_data + end_table;
            $(html_data).appendTo(".vidz");
            $("a.youtube").YouTubePopup({ autoplay: 1 });
        });
    });//]]>
</script>

任何人都知道如何获取其他视频?这个播放列表中有27个(我相信)。我对json没有多少经验,所以我有点卡住了。

感谢您的帮助!

1 个答案:

答案 0 :(得分:5)

您需要在请求中定义最大结果,默认情况下只返回25个结果。

http://code.google.com/apis/youtube/2.0/reference.html#max-resultssp

以下代码段返回28个结果:

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>   
<script type='text/javascript'>//<![CDATA[
    $(window).load(function () {
        var playListURL = 'http://gdata.youtube.com/feeds/api/playlists/A6A0F817C1771FE5?v=2&alt=json&max-results=50';
        var videoURL = 'http://www.youtube.com/watch?v=';
        $.getJSON(playListURL, function (data) {
            var list_data = "";
            var column_count = -1;
            var begin_table = "<table width='100%'><tr>";
            var end_table = "</table>";
            var html_data = "";
            $.each(data.feed.entry, function (i, item) {
                column_count = column_count + 1;
                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(column_count <= 3)
                    html_data += '<td><a class="youtube" href="#" rel="' + videoID + '" title="' + feedTitle + '"><img alt="' + feedTitle + '" src="' + thumb + '"></a></td>';
                else
                {
                    column_count = 0;
                    html_data += "</tr><tr>";
                    html_data += '<td><a class="youtube" href="#" rel="' + videoID + '" title="' + feedTitle + '"><img alt="' + feedTitle + '" src="' + thumb + '"></a></td>';
                }
            });
            html_data = begin_table + html_data + end_table;
            $(html_data).appendTo(".vidz");
            });
    });//]]>
</script>
</head>
<body>
<div class="vidz"></div>
</body>
</html>