0

I am trying my hand at searching youtube with the youtube API via Javascript, and as per the google docs on the matter:

<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>

this should return data on the song(query) all the small things. and it does! however what im trying to do with this data is grab the id from the json data, but as i am completely new to json (and js for that matter) i seem to be having a hard time doing this. viewing the page with the network tools i can clearly see the section that contains the id:

"id":{"$t":"tag:youtube.com,2008:video:9Ht5RZpzPqw"}

and i can clearly see the data that i want(9Ht5RZpzPqw) however no matter how much i scoure json tutorials it makes no sense to me, google seems to do json different than every tutorial ive read. I threw together a jsbin so you can see the results yourself: jsbin Anyone have anyideas? you can see in my code one of the many things i have tried to retrieve that data. Nothing I do seems to work though... Anyone have any ideas or pointers?

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"}}]}});
4

1 回答 1

0

的值$t不是 JSON,而是 Google 用来对 ID 信息进行编码的字符串。你可以这样做:

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

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

于 2013-05-24T19:02:35.567 回答