我正在尝试编写一个脚本来获取 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/