javascript - 使用Javascript从youtube API扫描JSON数据

标签 javascript jquery youtube youtube-api youtube-javascript-api

我正在尝试通过Javascript使用youtube API搜索youtube,并且按照有关此问题的google文档:

<div id="agenda"></div>

<script>
  function listEvents(root) {
    var feed = root.feed;
    var entries = feed.entry || [];
    var html = ['<ul>'];


    for (var i = 0; i < entries.length; ++i) {
      var entry = entries[i];
      console.log(entry);
      var title = (entry.title.type == 'html') ? entry.title.$t : escape(entry.title.$t);
      var start = (entry['gd$when']) ? entry['gd$when'][0].startTime : "";
      //var id = id.video;  
      //alert(id);

      html.push('<li>', start, ' ', title, '</li>');
    }

    html.push('</ul>');
    document.getElementById("agenda").innerHTML = html.join("");
  }
</script>

<script src="https://gdata.youtube.com/feeds/api/videos?q=all the small things&max-results=2&v=2&format=5&orderby=relevance&safeSearch=none&alt=json-in-script&callback=listEvents">
</script>

这应该返回有关歌曲(查询)所有小数据的数据。确实如此!但是,我试图用此数据处理的是从json数据中获取ID,但是由于我对json(和js而言)是完全陌生的,所以我似乎很难这么做。使用网络工具查看页面,我可以清楚地看到包含ID的部分:
"id":{"$t":"tag:youtube.com,2008:video:9Ht5RZpzPqw"}
而且我可以清楚地看到我想要的数据(9Ht5RZpzPqw),但是无论我对json教程进行多少精练对我来说都是没有道理的,google似乎所做的json与阅读的每个教程都不一样。我整理了一个jsbin,以便您自己查看结果:jsbin
谁有想法?您可以在我的代码中看到我尝试检索该数据的众多操作之一。我似乎什么都没做,但是...有任何想法或建议吗?
listEvents({"version":"1.0","encoding":"UTF-8","feed":{"xmlns$app":"http://www.w3.org/2007/app","xmlns":"http://www.w3.org/2005/Atom","xmlns$media":"http://search.yahoo.com/mrss/","xmlns$openSearch":"http://a9.com/-/spec/opensearch/1.1/","xmlns$gd":"http://schemas.google.com/g/2005","xmlns$yt":"http://gdata.youtube.com/schemas/2007","gd$etag":"W/\"DkEBRn08fip7I2A9WhBaFE8.\"","id":{"$t":"tag:youtube.com,2008:videos"},"updated":{"$t":"2013-05-24T19:44:17.376Z"},"category":[{"scheme":"http://schemas.google.com/g/2005#kind","term":"http://gdata.youtube.com/schemas/2007#video"}],"title":{"$t":"Videos matching: all the small things"},"logo":{"$t":"http://www.youtube.com/img/pic_youtubelogo_123x63.gif"},"link":[{"rel":"alternate","type":"text/html","href":"https://www.youtube.com"},{"rel":"http://schemas.google.com/g/2005#feed","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/videos?v=2"},{"rel":"http://schemas.google.com/g/2005#batch","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/videos/batch?v=2"},{"rel":"self","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/videos?alt=json-in-script&q=all+the+small+things&start-index=1&max-results=2&format=5&safeSearch=none&orderby=relevance&v=2"},{"rel":"service","type":"application/atomsvc+xml","href":"https://gdata.youtube.com/feeds/api/videos?alt=atom-service&v=2"},{"rel":"next","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/videos?alt=json-in-script&q=all+the+small+things&start-index=3&max-results=2&format=5&safeSearch=none&orderby=relevance&v=2"}],"author":[{"name":{"$t":"YouTube"},"uri":{"$t":"http://www.youtube.com/"}}],"generator":{"$t":"YouTube data API","version":"2.1","uri":"http://gdata.youtube.com"},"openSearch$totalResults":{"$t":1000000},"openSearch$startIndex":{"$t":1},"openSearch$itemsPerPage":{"$t":2},"entry":[{"gd$etag":"W/\"C0IMSH47eCp7I2A9WhBaFE8.\"","id":{"$t":"tag:youtube.com,2008:video:9Ht5RZpzPqw"},"published":{"$t":"2009-06-16T23:16:07.000Z"},"updated":{"$t":"2013-05-24T18:53:09.000Z"},"app$control":{"yt$state":{"$t":"Syndication of this video was restricted.","name":"restricted","reasonCode":"limitedSyndication"}},"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":"Music","label":"Music"}],"title":{"$t":"blink-182 - All The Small Things"},"content":{"type":"application/x-shockwave-flash","src":"https://www.youtube.com/v/9Ht5RZpzPqw?version=3&f=videos&app=youtube_gdata"},"link":[{"rel":"alternate","type":"text/html","href":"https://www.youtube.com/watch?v=9Ht5RZpzPqw&feature=youtube_gdata"},{"rel":"http://gdata.youtube.com/schemas/2007#video.responses","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/videos/9Ht5RZpzPqw/responses?v=2"},{"rel":"http://gdata.youtube.com/schemas/2007#video.related","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/videos/9Ht5RZpzPqw/related?v=2"},{"rel":"http://gdata.youtube.com/schemas/2007#uploader","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/users/f2WBemooP2gBBx3lrraNQw?v=2"},{"rel":"self","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/videos/9Ht5RZpzPqw?v=2"}],"author":[{"name":{"$t":"blink-182"},"uri":{"$t":"https://gdata.youtube.com/feeds/api/users/blink182VEVO"},"yt$userId":{"$t":"f2WBemooP2gBBx3lrraNQw"}}],"yt$accessControl":[{"action":"comment","permission":"allowed"},{"action":"commentVote","permission":"allowed"},{"action":"videoRespond","permission":"allowed"},{"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/9Ht5RZpzPqw/comments?v=2","countHint":47078}},"media$group":{"media$category":[{"$t":"Music","label":"Music","scheme":"http://gdata.youtube.com/schemas/2007/categories.cat"}],"media$content":[{"url":"https://www.youtube.com/v/9Ht5RZpzPqw?version=3&f=videos&app=youtube_gdata","type":"application/x-shockwave-flash","medium":"video","isDefault":"true","expression":"full","duration":173,"yt$format":5}],"media$credit":[{"$t":"blink182vevo","role":"uploader","scheme":"urn:youtube","yt$display":"blink-182","yt$type":"partner"}],"media$description":{"$t":"Music video by blink-182 performing All The Small Things. (C) 2000 Geffen Records.","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=9Ht5RZpzPqw&feature=youtube_gdata_player"},"media$restriction":[{"$t":"DE","type":"country","relationship":"deny"}],"media$thumbnail":[{"url":"http://i.ytimg.com/vi/9Ht5RZpzPqw/default.jpg","height":90,"width":120,"time":"00:01:26.500","yt$name":"default"},{"url":"http://i.ytimg.com/vi/9Ht5RZpzPqw/mqdefault.jpg","height":180,"width":320,"yt$name":"mqdefault"},{"url":"http://i.ytimg.com/vi/9Ht5RZpzPqw/hqdefault.jpg","height":360,"width":480,"yt$name":"hqdefault"},{"url":"http://i.ytimg.com/vi/9Ht5RZpzPqw/1.jpg","height":90,"width":120,"time":"00:00:43.250","yt$name":"start"},{"url":"http://i.ytimg.com/vi/9Ht5RZpzPqw/2.jpg","height":90,"width":120,"time":"00:01:26.500","yt$name":"middle"},{"url":"http://i.ytimg.com/vi/9Ht5RZpzPqw/3.jpg","height":90,"width":120,"time":"00:02:09.750","yt$name":"end"}],"media$title":{"$t":"blink-182 - All The Small Things","type":"plain"},"yt$duration":{"seconds":"173"},"yt$uploaded":{"$t":"2009-06-16T23:16:07.000Z"},"yt$uploaderId":{"$t":"UCf2WBemooP2gBBx3lrraNQw"},"yt$videoid":{"$t":"9Ht5RZpzPqw"}},"gd$rating":{"average":4.900098,"max":5,"min":1,"numRaters":144872,"rel":"http://schemas.google.com/g/2005#overall"},"yt$statistics":{"favoriteCount":"0","viewCount":"49388829"},"yt$rating":{"numDislikes":"3618","numLikes":"141254"}},{"gd$etag":"W/\"D0UCQH47eCp7I2A9WhBaFEw.\"","id":{"$t":"tag:youtube.com,2008:video:ahWmkV0mtvk"},"published":{"$t":"2007-09-19T10:10:21.000Z"},"updated":{"$t":"2013-05-24T17:07:41.000Z"},"app$control":{"yt$state":{"$t":"Syndication of this video was restricted.","name":"restricted","reasonCode":"limitedSyndication"}},"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":"Music","label":"Music"}],"title":{"$t":"blink 182 all the small things lyrics"},"content":{"type":"application/x-shockwave-flash","src":"https://www.youtube.com/v/ahWmkV0mtvk?version=3&f=videos&app=youtube_gdata"},"link":[{"rel":"alternate","type":"text/html","href":"https://www.youtube.com/watch?v=ahWmkV0mtvk&feature=youtube_gdata"},{"rel":"http://gdata.youtube.com/schemas/2007#video.responses","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/videos/ahWmkV0mtvk/responses?v=2"},{"rel":"http://gdata.youtube.com/schemas/2007#video.related","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/videos/ahWmkV0mtvk/related?v=2"},{"rel":"http://gdata.youtube.com/schemas/2007#uploader","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/users/KGRY-OpB0xZLqpTZNR418w?v=2"},{"rel":"self","type":"application/atom+xml","href":"https://gdata.youtube.com/feeds/api/videos/ahWmkV0mtvk?v=2"}],"author":[{"name":{"$t":"fuckudumbcuntimnot"},"uri":{"$t":"https://gdata.youtube.com/feeds/api/users/fuckudumbcuntimnot"},"yt$userId":{"$t":"KGRY-OpB0xZLqpTZNR418w"}}],"yt$accessControl":[{"action":"comment","permission":"allowed"},{"action":"commentVote","permission":"allowed"},{"action":"videoRespond","permission":"allowed"},{"action":"rate","permission":"allowed"},{"action":"embed","permission":"allowed"},{"action":"list","permission":"allowed"},{"action":"autoPlay","permission":"allowed"},{"action":"syndicate","permission":"denied"}],"gd$comments":{"gd$feedLink":{"rel":"http://gdata.youtube.com/schemas/2007#comments","href":"https://gdata.youtube.com/feeds/api/videos/ahWmkV0mtvk/comments?v=2","countHint":5681}},"media$group":{"media$category":[{"$t":"Music","label":"Music","scheme":"http://gdata.youtube.com/schemas/2007/categories.cat"}],"media$content":[{"url":"https://www.youtube.com/v/ahWmkV0mtvk?version=3&f=videos&app=youtube_gdata","type":"application/x-shockwave-flash","medium":"video","isDefault":"true","expression":"full","duration":159,"yt$format":5}],"media$credit":[{"$t":"fuckudumbcuntimnot","role":"uploader","scheme":"urn:youtube","yt$display":"fuckudumbcuntimnot"}],"media$description":{"$t":"awsome!!!!!!!!!!!11.","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=ahWmkV0mtvk&feature=youtube_gdata_player"},"media$restriction":[{"$t":"DE","type":"country","relationship":"deny"}],"media$thumbnail":[{"url":"http://i.ytimg.com/vi/ahWmkV0mtvk/default.jpg","height":90,"width":120,"time":"00:01:19.500","yt$name":"default"},{"url":"http://i.ytimg.com/vi/ahWmkV0mtvk/mqdefault.jpg","height":180,"width":320,"yt$name":"mqdefault"},{"url":"http://i.ytimg.com/vi/ahWmkV0mtvk/hqdefault.jpg","height":360,"width":480,"yt$name":"hqdefault"},{"url":"http://i.ytimg.com/vi/ahWmkV0mtvk/1.jpg","height":90,"width":120,"time":"00:00:39.750","yt$name":"start"},{"url":"http://i.ytimg.com/vi/ahWmkV0mtvk/2.jpg","height":90,"width":120,"time":"00:01:19.500","yt$name":"middle"},{"url":"http://i.ytimg.com/vi/ahWmkV0mtvk/3.jpg","height":90,"width":120,"time":"00:01:59.250","yt$name":"end"}],"media$title":{"$t":"blink 182 all the small things lyrics","type":"plain"},"yt$duration":{"seconds":"159"},"yt$uploaded":{"$t":"2007-09-19T10:10:21.000Z"},"yt$uploaderId":{"$t":"UCKGRY-OpB0xZLqpTZNR418w"},"yt$videoid":{"$t":"ahWmkV0mtvk"}},"gd$rating":{"average":4.934968,"max":5,"min":1,"numRaters":17130,"rel":"http://schemas.google.com/g/2005#overall"},"yt$statistics":{"favoriteCount":"0","viewCount":"6287584"},"yt$rating":{"numDislikes":"278","numLikes":"16852"}}]}});

最佳答案

$t的值不是JSON,而是Google用来编码有关ID信息的字符串。您可以执行以下操作:

var vidId = root.feed.id.$t.split('video:')[1]; 

这将从ID对象获取$t的值,然后在video:的右侧查找字符串。

关于javascript - 使用Javascript从youtube API扫描JSON数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16741495/

相关文章:

javascript - 显示和隐藏带链接的 div

javascript - D3 Force Layout - 子图聚类特性

javascript - Jquery中循环以 'req-'开头的元素

jquery - 如何创建具有不同图像高度和宽度的图像网格 slider (砌体 slider )

php - YouTube 没有显示为推荐人?

flash - 如何让 YouTube 的嵌入代码针对 HTML5 进行验证?

javascript - Spotify 应用程序 API : any more documentation?

javascript - 按姓氏对数组排序

javascript - 如何控制基于 &lt;iframe&gt; 的 YouTube 播放器?

javascript - setState() 不触发重新渲染