【问题标题】:YouTube user feed parsing from JSON从 JSON 解析 YouTube 用户提要
【发布时间】:2013-03-27 07:08:23
【问题描述】:

我正在尝试制作一个脚本来获取特定用户在 YouTube 上最近 2 次上传的 JSON 提要。我尝试使用这个脚本

<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 的提要可能出了什么问题?

【问题讨论】:

    标签: javascript jquery json youtube


    【解决方案1】:

    您已将来自 YouTube 的 API 返回放入一个 JavaScript 对象 data,但请记住,YouTube 还将返回的对象封装在一个名为 data 的对象中。

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

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

    ...实际上应该是:

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

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

    【讨论】:

    • 非常感谢,帮了大忙
    猜你喜欢
    • 2012-07-02
    • 2013-05-14
    • 1970-01-01
    • 2011-11-30
    • 2015-10-23
    • 1970-01-01
    • 2012-10-19
    • 2013-03-09
    • 2014-08-02
    相关资源
    最近更新 更多