解析youtube返回的json数据

时间:2014-01-09 20:01:41

标签: javascript json

我是新蜜蜂尝试一些javascript来读取下面的json数据,我想解析youtube api返回的json数据。

json数据的网址https://gdata.youtube.com/feeds/api/videos/SPtEUAFm6Yk?v=2&alt=json

此网址返回的数据

{"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"}}}

我的代码来解析这个

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

我从$ .getJson(mydata)中检索的任何属性都未定义。

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

请帮助解析上述对象。

1 个答案:

答案 0 :(得分:2)

$.getJSON(mydata).version不正确,这就是为什么你得到一个未定义的。

试试这个:

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

console.log会显示在您的浏览器控制台窗口中。