使用Javascript从youtube API扫描JSON数据



我正在尝试用Javascript的youtube API搜索youtube,并根据谷歌文档对此事:

<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教程对我来说没有意义,谷歌似乎做json不同于我读过的每一个教程。我拼凑了一个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,它是一个字符串谷歌用来编码信息的ID。你可以这样做:

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

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

相关内容

  • 没有找到相关文章

最新更新