1

I am new bee trying some javascript to read the json data below , I want to parse json data returned by youtube api.

Url for json data https://gdata.youtube.com/feeds/api/videos/SPtEUAFm6Yk?v=2&alt=json

the data returned by this url

{"version":"1.0","encoding":"UTF-8","entry":{"xmlns":"http://www.w3.org/2005/Atom","xmlns$media":"http://search.yahoo.com/mrss/","xmlns$gd":"http://schemas.google.com/g/2005","xmlns$yt":"http://gdata.youtube.com/schemas/2007","gd$etag":"W/\"CkIDSH47eCp7I2A9Wh5VFks.\"","id":{"$t":"tag:youtube.com,2008:video:SPtEUAFm6Yk"},"published":{"$t":"2010-02-13T17:09:09.000Z"},"updated":{"$t":"2013-12-10T07:29:39.000Z"},"category":[{"scheme":"http://schemas.google.com/g/2005#kind","term":"http://gdata.youtube.com/schemas/2007#video"},{"scheme":"http://gdata.youtube.com/schemas/2007/categories.cat","term":"Travel","label":"Travel & Events"}],"title":{"$t":"Atlanta Winter"},"content":{"type":"application/x-shockwave-flash","src":"https://www.youtube.com/v/SPtEUAFm6Yk?version=3&f=videos&app=youtube_gdata"},"link":[{"rel":"alternate","type":"text/html","href":"https://www.youtube.com/watch?v=SPtEUAFm6Yk&feature=youtube_gdata"},{"rel":"http://gdata.youtube.com/schemas/2007#video.related","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/videos/SPtEUAFm6Yk/related?v=2"},{"rel":"http://gdata.youtube.com/schemas/2007#mobile","type":"text/html","href":"https://m.youtube.com/details?v=SPtEUAFm6Yk"},{"rel":"http://gdata.youtube.com/schemas/2007#uploader","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/users/UGJ8B_nJw0dEyWyLZi5m4Q?v=2"},{"rel":"self","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/videos/SPtEUAFm6Yk?v=2"}],"author":[{"name":{"$t":"Jason Gifford"},"uri":{"$t":"https://gdata.youtube.com/feeds/api/users/jgiff76"},"yt$userId":{"$t":"UGJ8B_nJw0dEyWyLZi5m4Q"}}],"yt$accessControl":[{"action":"comment","permission":"allowed"},{"action":"commentVote","permission":"allowed"},{"action":"videoRespond","permission":"moderated"},{"action":"rate","permission":"allowed"},{"action":"embed","permission":"allowed"},{"action":"list","permission":"allowed"},{"action":"autoPlay","permission":"allowed"},{"action":"syndicate","permission":"allowed"}],"gd$comments":{"gd$feedLink":{"rel":"http://gdata.youtube.com/schemas/2007#comments","href":"https://gdata.youtube.com/feeds/api/videos/SPtEUAFm6Yk/comments?v=2","countHint":0}},"media$group":{"media$category":[{"$t":"Travel","label":"Travel & Events","scheme":"http://gdata.youtube.com/schemas/2007/categories.cat"}],"media$content":[{"url":"https://www.youtube.com/v/SPtEUAFm6Yk?version=3&f=videos&app=youtube_gdata","type":"application/x-shockwave-flash","medium":"video","isDefault":"true","expression":"full","duration":224,"yt$format":5},{"url":"rtsp://r3---sn-p5qlsu7z.c.youtube.com/CiILENy73wIaGQmJ6WYBUET7SBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp","type":"video/3gpp","medium":"video","expression":"full","duration":224,"yt$format":1},{"url":"rtsp://r3---sn-p5qlsu7z.c.youtube.com/CiILENy73wIaGQmJ6WYBUET7SBMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp","type":"video/3gpp","medium":"video","expression":"full","duration":224,"yt$format":6}],"media$credit":[{"$t":"jgiff76","role":"uploader","scheme":"urn:youtube","yt$display":"Jason Gifford"}],"media$description":{"$t":"A rare snow that sticks in the south.  Reminds me of being a kid.  Feels more like Michigan than Georgia.","type":"plain"},"media$keywords":{},"media$license":{"$t":"youtube","type":"text/html","href":"http://www.youtube.com/t/terms"},"media$player":{"url":"https://www.youtube.com/watch?v=SPtEUAFm6Yk&feature=youtube_gdata_player"},"media$restriction":[{"$t":"DE","type":"country","relationship":"deny"}],"media$thumbnail":[{"url":"https://i1.ytimg.com/vi/SPtEUAFm6Yk/default.jpg","height":90,"width":120,"time":"00:01:52","yt$name":"default"},{"url":"https://i1.ytimg.com/vi/SPtEUAFm6Yk/mqdefault.jpg","height":180,"width":320,"yt$name":"mqdefault"},{"url":"https://i1.ytimg.com/vi/SPtEUAFm6Yk/hqdefault.jpg","height":360,"width":480,"yt$name":"hqdefault"},{"url":"https://i1.ytimg.com/vi/SPtEUAFm6Yk/1.jpg","height":90,"width":120,"time":"00:00:56","yt$name":"start"},{"url":"https://i1.ytimg.com/vi/SPtEUAFm6Yk/2.jpg","height":90,"width":120,"time":"00:01:52","yt$name":"middle"},{"url":"https://i1.ytimg.com/vi/SPtEUAFm6Yk/3.jpg","height":90,"width":120,"time":"00:02:48","yt$name":"end"}],"media$title":{"$t":"Atlanta Winter","type":"plain"},"yt$aspectRatio":{"$t":"widescreen"},"yt$duration":{"seconds":"224"},"yt$uploaded":{"$t":"2010-02-13T17:09:09.000Z"},"yt$uploaderId":{"$t":"UCUGJ8B_nJw0dEyWyLZi5m4Q"},"yt$videoid":{"$t":"SPtEUAFm6Yk"}},"gd$rating":{"average":5.0,"max":5,"min":1,"numRaters":1,"rel":"http://schemas.google.com/g/2005#overall"},"yt$statistics":{"favoriteCount":"0","viewCount":"356"},"yt$rating":{"numDislikes":"0","numLikes":"1"}}}

my code to parse this

alert($.getJSON(mydata));
  alert($.getJSON(mydata).version);

I get undefined for any property I retrieve from the $.getJson(mydata).

jsfiddle link http://jsfiddle.net/fachhoch/gE96Q/1/

Please help how to parse the above object.

1

1 Answer 1

2

$.getJSON(mydata).version is not correct and that's why you're getting an undefined.

Try this:

$.getJSON("https://gdata.youtube.com/feeds/api/videos/SPtEUAFm6Yk?v=2&alt=json", function( data ) {
     console.log(data.version);
});

console.log shows up in your browsers console window.

Sign up to request clarification or add additional context in comments.

5 Comments

I wan to render the json data in my underscore template, so I want to call the url get the json object pass it to the view and call properties using dot notation , please advice how to ?,the one you gave works but I cannot go with that unless I retrieve all the properties and set it to some other object in the callback function for $.getJSON('...url', function(){}).
I tried this <code> var myObject; $.getJSON("gdata.youtube.com/feeds/api/videos/SPtEUAFm6Yk?v=2&alt=json", function( data ) { myObject=data; // here it prints fine. console.log(myObject.entry.content.src); }); // this says undefined console.log(myObject); </code> pleae advie how to capture the result inot an object and use it later to retriee properties
the callbacks are async. $.getJSON executes then immediately goes to console.log(myObject). Later when $.getJSON returns with the youtube data it then calls the callback function. By that time the console.log(myObject) already ran. jsfiddle: jsfiddle.net/gE96Q/4
I am building app with backbone js, my thought is call the json data set it to my model and use it in the view , if the getjson is async then obviously my model will contain null and my template will render it as null, please advice what developers do to overcome this aync behaviour I am new bee to javascript
Suggestion, don't use backbone until you get a good grasp of javascript and aync callbacks. Consider reading this book, Javascript: The Good Parts. It's the best place to start learning JS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.