2014-01-09 135 views
1

我是新蜂嘗試一些javascript讀取下面的json數據,我想解析youtube api返回的json數據。解析youtube返回的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鏈接http://jsfiddle.net/fachhoch/gE96Q/1/

請幫助如何解析上述目的。

+0

可能的重複http://stackoverflow.com/q/14220321/1348195 –

回答

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在您的瀏覽器的控制檯窗口中顯示出來。

+0

我想呈現我的下劃線模板中的json數據,所以我想調用url獲取json對象將它傳遞給視圖並調用使用點符號的屬性,請教如何?,你給的作品,但我不能去,除非我檢索所有的屬性,並將其設置爲$ .getJSON回調函數中的其他對象('... url' ,function(){})。 – user884424

+0

我試過 var myObject; $.getJSON("https://gdata.youtube.com/feeds/api/videos/SPtEUAFm6Yk?v=2&alt=json", function(data) { myObject=data; // here it prints fine. \t \t console.log(myObject.entry.content.src); }); // this says undefined console.log(myObject); 請注意如何捕獲對象的結果並稍後使用它來檢索屬性 – user884424

+0

回調是異步的。 $ .getJSON執行,然後立即進入console.log(myObject)。稍後當$ .getJSON與youtube數據一起返回時,它會調用回調函數。那時候console.log(myObject)已經運行了。 jsfiddle:http://jsfiddle.net/gE96Q/4/ –