javascript - YouTube 用户提要从 JSON 解析

标签 javascript jquery json youtube

我正在尝试编写一个脚本来获取 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 的提要可能出了什么问题?

最佳答案

您已将 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/

关于javascript - YouTube 用户提要从 JSON 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15824254/

相关文章:

javascript - 为什么我对两个选择器的比较永远不起作用?

python - 在 Python 中将 JSON 转换为 SQLite - 如何将 json 键正确映射到数据库列?

javascript - Dhtmlx Scheduler 时间线为周末上色

javascript - 如何使用 JavaScript 添加选定数量的 TD?

php - 如何使用 php 和 mysql 将创建的行值上的多个 jquery 插入到数据库中?

javascript - 从数组中删除项目会跳过某些项目

javascript - json.stringify 不处理对象方法

python - 如何解析 df 中的 json 列,我们使用选定的键附加新列

javascript - jQuery - 将方法应用于多个对象

javascript - 从 JSONP 请求数据