Javascript 遍历附加的json文件

Javascript 遍历附加的json文件,javascript,html,json,Javascript,Html,Json,我有以下代码: function heatMapRange() { var script1 = document.createElement('script'); script1.src = 'allCoords.js'; document.getElementsByTagName('head')[0].appendChild(script1); } 将allCoords.js文件追加到上面: allCoords_callback({ "coordinates": [

我有以下代码:

function heatMapRange() {
  var script1 = document.createElement('script');
  script1.src = 'allCoords.js';
  document.getElementsByTagName('head')[0].appendChild(script1);
}
将allCoords.js文件追加到上面:

allCoords_callback({
  "coordinates": [
    [50.1729677, 12.6692243, 580000],
    [50.001168, 14.4270033, 2895000],
    [50.6988037, 13.9384015, 945000],
    [50.1218161, 14.4824004, 409900],
    [49.470061, 17.0937597, 1499000],
    [49.8509959, 18.5593087, 380000]
  ]
});
我想要的是使用如下内容迭代此数据:

function allCoords_callback(results1) {
  for (var i = 0; i < results1.coordinates.length; i++) {
    alert(results1.coordinates[i]);
  }
}
function heatMapRange(){
  var request = new XMLHttpRequest();
  request.open('GET', '/allCoords.js', true);
  request.onload = function () {
    if (request.status >= 200 && request.status < 400) {
      // Success!
      var data = JSON.parse(request.responseText);
      // process the data in the response, like
      // iterating through the list of coordinates
      data.coordinates.map(function(coordinate) { alert(coordinate); })
    } else {
      // We reached our target server, but it returned an error
    }
  }
  request.error = function () {
    // There was a connection error of some sort
  }
  request.send();
}
函数allCoords\u回调(结果1){
对于(var i=0;i

有可能吗?

您可以使用在javascript中迭代数组

在您的示例中,将出现以下情况:

results1.coordinates.map(function(coordinate) { alert(coordinate); })
这是关于迭代部分

然后,另一个主题是如何获得需要处理的JSON。在上给出的示例中,他们使用的只是因为这是。另一种获取数据的方法是(也称为AJAX)。这是一种更常见的做法,如果可能的话,我建议使用它

在您的情况下,我将重新编写代码,使其看起来像这样:

function allCoords_callback(results1) {
  for (var i = 0; i < results1.coordinates.length; i++) {
    alert(results1.coordinates[i]);
  }
}
function heatMapRange(){
  var request = new XMLHttpRequest();
  request.open('GET', '/allCoords.js', true);
  request.onload = function () {
    if (request.status >= 200 && request.status < 400) {
      // Success!
      var data = JSON.parse(request.responseText);
      // process the data in the response, like
      // iterating through the list of coordinates
      data.coordinates.map(function(coordinate) { alert(coordinate); })
    } else {
      // We reached our target server, but it returned an error
    }
  }
  request.error = function () {
    // There was a connection error of some sort
  }
  request.send();
}

这种从服务器获取数据的方法更符合业界使用的最佳实践。这只是使用vanillaJS XMLHttpRequest的简单示例。有很多库可以简化此操作。更好的是,我们正在着手解决获取资源的问题。

顶部的代码很有效,问题是我在google chrome中禁用了警报。所以关闭选项卡并重新打开页面就成功了。

那么我应该把这一行放到allCoords\u回调中?因为回调永远不会运行OK,这是另一个问题。我不完全明白为什么要通过附加脚本而不是使用(也称为AJAX)来获取坐标对象。你能给出一点你为什么这么做的背景吗?我正在使用GoogleMapsAPI创建热图,使用这个例子:我能够创建热图。我的问题是,我需要添加另一个包含json数据的子对象来创建另一个热图层。我正在执行与本例相同的操作,但回调从未运行。请尝试将您的
allCords\u回调
founction重命名为
eqfeed\u回调
,然后告诉我是什么happens@nocturne,我已经更新了答案,以涵盖您问题的获取数据部分,而不仅仅是迭代部分。请告诉我这对你是否有意义。