Javascript 使用JSON从Last.fm API提取用户播放计数
简而言之,我想在我的网站上显示Last.fm用户playcount数字(请参阅上的“playcount”)。我试过以下方法,但坦白说,我不知道我的思路是否正确Javascript 使用JSON从Last.fm API提取用户播放计数,javascript,json,api,last.fm,Javascript,Json,Api,Last.fm,简而言之,我想在我的网站上显示Last.fm用户playcount数字(请参阅上的“playcount”)。我试过以下方法,但坦白说,我不知道我的思路是否正确 $(document).ready(function() { $.getJSON("http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user=XXX&api_key=XXX&limit=5&format=json&callback=?",
$(document).ready(function() {
$.getJSON("http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user=XXX&api_key=XXX&limit=5&format=json&callback=?", function(data) {
var html = '';
$.each(data.user.playcount, function(i, item) {
html += "<p>" + item.playcount + "</p>";
});
$('#count').append(html);
});
});
$(文档).ready(函数(){
$.getJSON(“http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user=XXX&api_key=XXX&limit=5&format=json&callback=?,函数(数据){
var html='';
$.each(data.user.playcount,函数(i,项){
html+=“”+item.playcount+“”;
});
$('#count').append(html);
});
});
内部foreach循环是不必要的,user.getInfo
毕竟只返回一个用户的信息。以下方面应起作用:
$(document).ready(function() {
$.getJSON("http://ws.audioscrobbler.com/2.0/?method=user.getInfo&user=XXX&api_key=XXX&format=json", function(data) {
var html = "<p>" + data.user.playcount + "</p>";
$('#count').append(html);
});
});
$(文档).ready(函数(){
$.getJSON(“http://ws.audioscrobbler.com/2.0/?method=user.getInfo&user=XXX&api_key=XXX&format=json,函数(数据){
var html=“”+data.user.playcount+””;
$('#count').append(html);
});
});
为了简洁起见,我还从URL中删除了限制
和回调
参数。你真是个天才!非常感谢@user2758929,它工作得非常好。