0

我正在尝试从 youtube api 返回结果。我的代码如下:

function Vget(strt){

$.get("https://gdata.youtube.com/feeds/api/videos", { q: "soccer, 'start-index':strt,     'max-results':"5", v:"2",alt:"jsonc" },
 function(dta){
 var dt= $.parseJSON(dta);
var res= dt.data.items;
var tr=  dt.data.totalItems;

if ( (dt.data) && tr>0){
  for (i=0;i<5;i++){
 //do something with results
  }


}
})
}

但是“var res=dt.data.items;”这一行 在 Chrome 中给我一个错误:“未捕获的 TypeError:无法读取 null 的属性‘数据’”。

Firebug 没有显示任何错误...

编辑:

我尝试注释掉“var dt= $.parseJSON(dta);” 并将“dt.data”替换为“dta.data。并直接使用 dta 对象。这在 Chrome 上效果很好,但随后 Firefox 给出了一个错误:“dta.data in undefined”!

编辑 2:这是返回的 json 的样子:

{"apiVersion":"2.1","data":{"updated":"2012-09-20T16:45:14.080Z","totalItems":153820,"startIndex":1,"itemsPerPage":2,"items":[{"id":"PO2b3cggqs0","uploaded":"2009-05-04T05:50:37.000Z","updated":"2012-09-20T03:28:49.000Z","uploader":"jbtdotcom","category":"Music","title":"Zebra - Official Video","description":"Official video clip for the JBT song, 'Zebra' from the album 'Sunrise Over Sea'. www.johnbutlertrio.com","thumbnail":{"sqDefault":"http://i.ytimg.com/vi/PO2b3cggqs0/default.jpg","hqDefault":"http://i.ytimg.com/vi/PO2b3cggqs0/hqdefault.jpg"},"player":{"default":"https://www.youtube.com/watch?v=PO2b3cggqs0&feature=youtube_gdata_player","mobile":"https://m.youtube.com/details?v=PO2b3cggqs0"},"content":{"5":"https://www.youtube.com/v/PO2b3cggqs0?version=3&f=videos&app=youtube_gdata","1":"rtsp://v8.cache2.c.youtube.com/CiILENy73wIaGQnNqiDI3ZvtPBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp","6":"rtsp://v3.cache1.c.youtube.com/CiILENy73wIaGQnNqiDI3ZvtPBMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"},"duration":240,"rating":4.9585986,"likeCount":"4351","ratingCount":4396,"viewCount":1114536,"favoriteCount":5266,"commentCount":638,"accessControl":{"comment":"allowed","commentVote":"allowed","videoRespond":"moderated","rate":"allowed","embed":"allowed","list":"allowed","autoPlay":"allowed","syndicate":"allowed"}},{"id":"fljKx9nvrL4","uploaded":"2012-01-19T14:00:00.000Z","updated":"2012-09-20T06:03:17.000Z","uploader":"rhettandlink","category":"Entertainment","title":"Dope Zebra - Rhett & Link (Official Original Video) LMFAO","description":"This zebra is dope. Extended song on iTunes: dft.ba ** Official T-SHIRT: dft.ba Featured in LMFAO's Sorry For Party Rockin' music video and in their American Idol live performance on 4/19/12 and Billboard Music Awards on 5/20/12. You might have also spotted him on America's Got Talent...","thumbnail":{"sqDefault":"http://i.ytimg.com/vi/fljKx9nvrL4/default.jpg","hqDefault":"http://i.ytimg.com/vi/fljKx9nvrL4/hqdefault.jpg"},"player":{"default":"https://www.youtube.com/watch?v=fljKx9nvrL4&feature=youtube_gdata_player","mobile":"https://m.youtube.com/details?v=fljKx9nvrL4"},"content":{"5":"https://www.youtube.com/v/fljKx9nvrL4?version=3&f=videos&app=youtube_gdata","1":"rtsp://v1.cache7.c.youtube.com/CiILENy73wIaGQm-rO_Zx8pYfhMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp","6":"rtsp://v1.cache6.c.youtube.com/CiILENy73wIaGQm-rO_Zx8pYfhMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"},"duration":109,"aspectRatio":"widescreen","rating":4.958495,"likeCount":"55842","ratingCount":56427,"viewCount":4658199,"favoriteCount":32961,"commentCount":12834,"accessControl":{"comment":"allowed","commentVote":"allowed","videoRespond":"denied","rate":"allowed","embed":"allowed","list":"allowed","autoPlay":"allowed","syndicate":"allowed"}}]}}
4

1 回答 1

0

尝试将json写入控制台并检查数据的格式..

function(dta){
 console.log(dta); // Before Parsing
 var dt= $.parseJSON(dta);
 console.log(dt);  //After Parsing

看起来您没有正确访问数据

于 2012-09-20T15:47:55.963 回答