如何在javascript中使用for循环检索多个JSON对象

如何在javascript中使用for循环检索多个JSON对象,javascript,json,last.fm,Javascript,Json,Last.fm,我一直在使用Last.fm API和JSON,在过去的12个月里,我一直在尝试按月检索用户的顶级艺术家。我试图设置一个for循环,每个月遍历一次,然后提取对应于该月的相关JSON数据,但从我所能看出,for循环的运行速度似乎比JSON调用快得多 我正在使用Felix Bruns的last.fm javascript API 我检查了控制台,没有记录月份值,只有12个。我还收到一个未捕获的引用错误“json未定义” 我试着四处寻找解决方案,但我所有的搜索结果都是如何循环API调用的结果,而我正在寻

我一直在使用Last.fm API和JSON,在过去的12个月里,我一直在尝试按月检索用户的顶级艺术家。我试图设置一个for循环,每个月遍历一次,然后提取对应于该月的相关JSON数据,但从我所能看出,for循环的运行速度似乎比JSON调用快得多

我正在使用Felix Bruns的last.fm javascript API

我检查了控制台,没有记录月份值,只有12个。我还收到一个未捕获的引用错误“json未定义”

我试着四处寻找解决方案,但我所有的搜索结果都是如何循环API调用的结果,而我正在寻找如何编写检索多个JSON对象的循环

<script type="text/javascript">

  var apiKey = "b0da1774db3d010f62b11f67c4de0667";
  var secret = "0baa4b10c807acc847128599680679a7";

  var lastfm = new LastFM({
    apiKey : apiKey,
    secret : secret,
    cache : undefined
  });

  var lastfm_2 = new LastFM({
    apiKey : apiKey,
    secret : secret,
    cache : undefined
  });

  $(document).ready(function() {
    $("#submit").click(function() {
      var username = $("#username").val();
      var text = "";
      if (username) {
        $("#title").html("Your Most Played Artist by Month");
        $("#title").css("color", "#222");
        // Get top artists for each month
        var topArtistsByMonth = new Array();
        for (var month = 0; month < 12; month++) {
          lastfm.user.getTopArtists({user: username, period: "1month", limit: 15, page: month + 1}, {success: function(data) {
            topArtistsByMonth.push(data.topartists);
            console.log("Month " + month + ": " + data.topartists);
          }});
        }
      } else {
        alert("No username");
      }
    });
  });

</script>

var apiKey=“B0DA1774DB3B3D010F62B11F67C4DE0667”;
var secret=“0baa4b10c807acc847128599680679a7”;
var lastfm=新的lastfm({
阿皮基:阿皮基,
秘密:秘密,
缓存:未定义
});
var lastfm_2=新的lastfm({
阿皮基:阿皮基,
秘密:秘密,
缓存:未定义
});
$(文档).ready(函数(){
$(“#提交”)。单击(函数(){
var username=$(“#username”).val();
var text=“”;
如果(用户名){
$(“#title”).html(“每月最受欢迎的艺术家”);
$(“#title”).css(“颜色”,“#222”);
//每个月获得顶级艺术家
var topArtistsByMonth=新数组();
对于(变量月=0;月<12;月++){
getTopArtists({user:username,句点:“1个月”,限制:15,页面:月+1},{success:function(数据){
topArtistsByMonth.push(data.topartists);
日志(“月”+月+:“+数据.topartists);
}});
}
}否则{
警报(“无用户名”);
}
});
});

任何帮助都将不胜感激,谢谢

getTopArtists
是异步的,因此调用它只会启动请求;它不会等待它完成。回调就是你知道什么时候结束的。这意味着您的
for
循环并行地将它们全部触发,然后在完成后收集结果。但是,由于它们可以以任何顺序完成,
topArtistsByMonth
不保证以任何顺序完成。要解决这个问题,您可能需要使用显式索引,而不是使用
push

for(var month = 0; month < 12; month++) {
    // We need to use an anonymous function to capture the current value of month
    // so we don't end up capturing the reference to month that the for loop is
    // using (which, by the time the callbacks complete, will always be 12.)
    (function(month) {
        lastfm.user.getTopArtists({user: username, period: "1month", limit: 15, page: month + 1}, {success: function(data) {
            topArtistsByMonth[month] = data.topartists;
            console.log("Month " + month + ": " + data.topartists);
        }});
    })(month);
}
for(变量月=0;月<12;月++){
//我们需要使用匿名函数来捕获月份的当前值
//因此,我们最终不会捕获for循环所在月份的引用
//使用(在回调完成时,该值始终为12。)
(功能(月){
getTopArtists({user:username,句点:“1个月”,限制:15,页面:月+1},{success:function(数据){
TopPartistsByMonth[月]=data.topartists;
日志(“月”+月+:“+数据.topartists);
}});
})(月);
}

如果您想知道所有数据是什么时候下载的,那么需要另一个变量来跟踪到目前为止有多少数据已经下载完毕。每次调用回调时,您都需要增加该值,并查看是否达到12。当它出现时,所有的数据都被下载。

你的页面上的脚本中包含了吗?@soulcheck:现代浏览器内置了
JSON
global。我怀疑(可能)缺少
json2.js
是问题所在。@icktoofay根据错误消息OP gets判断,它可能也是OPs问题,最明显的不是传入响应的顺序,因为这样至少日志中会有一些内容。缺少JSON是一个问题,最近IE8处于兼容模式。啊,链接很糟糕-另一方面,那里也有console.log,这表明了一些现代的东西:)非常感谢!谢谢你的解释,真的很有帮助。