javascript - 解析youtube返回的json数据

标签 javascript json

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

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

这个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"}}}

我的代码来解析这个

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

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

jsfiddle 链接 http://jsfiddle.net/fachhoch/gE96Q/1/

请帮助如何解析上述对象。

最佳答案

$.getJSON(mydata).version 不正确,这就是您得到未定义的原因。

试试这个:

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

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

关于javascript - 解析youtube返回的json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21029985/

相关文章:

javascript - 使用排序函数对数组进行排序会返回错误

javascript - 将 div 内容存储在 PHP 变量中

javascript - 下拉组件填充数据,只有虚拟数据有效

arrays - 如果单元素解码失败,Swift JSONDecode 解码数组将失败

javascript - 更改 Gmail 中的字体系列

javascript - Bootgrid 模式中的文本未正确对齐

java - com.fasterxml.jackson.core.JsonParseException : Unrecognized character escape 'U' (code 85)

python - 函数在应该返回 True 时返回 false。编码问题?

json - JSONException错误Grails

javascript - 赋予键码 2 个不同的功能