hamama
hamama

Reputation: 121

YouTube user feed parsing from JSON

I am trying to make a script to get the JSON feed of a specific user latest 2 uploads on YouTube. I tried to use this script

<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);

});

I have been using a similar script for parsing a flickr's JSON and it's working fine.. What might be going wrong with YouTube's feed?

Upvotes: 0

Views: 815

Answers (1)

Julian H. Lam
Julian H. Lam

Reputation: 26124

You've placed the API return from YouTube into a javascript object data, but keep in mind that YouTube also encapsulates the returned object in an object called data.

Therefore, in line 14, when you begin iterating through the data set:

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

... should actually be:

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

Here is your code (corrected) placed into a jsFiddle: http://jsfiddle.net/Up3W7/

Upvotes: 2

Related Questions