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 で「Uncaught 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 に答える