YouTube用户Feed从JSON解析

时间:2013-04-05 01:00:10

标签: javascript jquery json youtube

我正在尝试创建一个脚本,以便在YouTube上获取特定用户最新2次上传的JSON Feed。我试图使用这个脚本

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
<script type="text/javascript">
// Set variables needed for query
var URL = "https://gdata.youtube.com/feeds/api/users/";
var UserName = "MyUsername";
var jsonFormat = "/uploads?v=2&alt=jsonc&max-results=2";
// Construct the JSON feed of the YouTube user's channel
var ajaxURL = URL + UserName + jsonFormat;
// Get the last videos of the YouTube account, parse it into HTML code
$.getJSON(ajaxURL, function(data){
     var htmlString = "";

    $.each(data.items, function(i,item){       
        // Here's where we piece together the HTML
        htmlString += '<iframe class="videos" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/';
        htmlString += item.id;
        htmlString += '?autoplay=0" frameborder="0"></iframe>';
    });

    // Pop our HTML in the #videos DIV
    $('#videos').html(htmlString);

});

我一直在使用类似的脚本来解析flickr的JSON,它工作得很好.YouTube的Feed会出现什么问题?

1 个答案:

答案 0 :(得分:2)

您已将YouTube返回的YouTube返回到javascript对象data,但请注意,YouTube还会将返回的对象封装在名为data的对象中。

因此,在第14行中,当您开始遍历数据集时:

$.each(data.items, function(i,item){ 

......实际应该是:

$.each(data.data.items, function(i,item){ 

以下是您的代码(已更正)放入jsFiddle:http://jsfiddle.net/Up3W7/

相关问题