无法读取JSON数据

时间:2016-11-18 10:26:06

标签: javascript json

我正在尝试从我拥有的json文件输出数据但是我得到错误TypeError:无法读取未定义的属性“0”。这是我的脚本和json代码...

var request = new XMLHttpRequest();
request.open('GET', 'outpuData.json', true);

request.onload = function() {
  if (request.status >= 200 && request.status < 400) {
    // Success!
    //var data = JSON.parse(request.responseText);
    
  } else {
    // We reached our target server, but it returned an error

  }
};

request.onerror = function() {
  // There was a connection error of some sort
};

request.send();

console.log(request[0].year);

[
  {
    "temp": 1.9,
    "lat": 58.233,
    "long": 5.25,
    "jday": 56.5,
    "year": 1867
  },
  {
    "temp": 1.9,
    "lat": 58.233,
    "long": 5.25,
    "jday": 57.3,
    "year": 1867
  },
  {
    "temp": 3.1,
    "lat": 58.233,
    "long": 5.25,
    "jday": 57.83,
    "year": 1867
  },
  {
    "temp": 3.1,
    "lat": 58.233,
    "long": 5.25,
    "jday": 58.3,
    "year": 1867
  },
  {
    "temp": 2.5,
    "lat": 58.994,
    "long": 4.211,
    "jday": 58.83,
    "year": 1867
  },
  {
    "temp": 3.1,
    "lat": 59.361,
    "long": 4.153,
    "jday": 59.3,
    "year": 1867
  },
  {
    "temp": 3.1,
    "lat": 59.922,
    "long": 3.672,
    "jday": 59.67,
    "year": 1867
  },
  {
    "temp": 7.5,
    "lat": 61.678,
    "long": 1.828,
    "jday": 60.3,
    "year": 1867
  },
  {
    "temp": 7.7,
    "lat": 62.6,
    "long": 1.347,
    "jday": 60.67,
    "year": 1867
  },
  {
    "temp": 6.9,
    "lat": 64.533,
    "long": 1.27,
    "jday": 61.3,
    "year": 1867
  },
  {
    "temp": 6.2,
    "lat": 65.672,
    "long": 1.333,
    "jday": 61.83,
    "year": 1867
  },
  {
    "temp": 5,
    "lat": 66.656,
    "long": 1.458,
    "jday": 62.3,
    "year": 1867
  },
  {
    "temp": 5.6,
    "lat": 67.172,
    "long": 1.77,
    "jday": 62.67,
    "year": 1867
  },
  {
    "temp": 3,
    "lat": 67.928,
    "long": 2.847,
    "jday": 63.3,
    "year": 1867
  },
  {
    "temp": 5.4,
    "lat": 68.367,
    "long": 3.489,
    "jday": 63.67,
    "year": 1867
  },
  {
    "temp": 5,
    "lat": 69.367,
    "long": 4.978,
    "jday": 64.3,
    "year": 1867
  },
  {
    "temp": 5,
    "lat": 69.717,
    "long": 5.2,
    "jday": 64.67,
    "year": 1867
  },
  {
    "temp": 4.4,
    "lat": 70.117,
    "long": 4.6,
    "jday": 65.3,
    "year": 1867
  },
  {
    "temp": 4.4,
    "lat": 70.333,
    "long": 4.144,
    "jday": 65.67,
    "year": 1867
  },
  {
    "temp": 3.7,
    "lat": 70.8,
    "long": 2.922,
    "jday": 66.3,
    "year": 1867
  },
  {
    "temp": 3.1,
    "lat": 71,
    "long": 2.072,
    "jday": 66.67,
    "year": 1867
  },
  {
    "temp": 3.1,
    "lat": 71.333,
    "long": -0.106,
    "jday": 67.3,
    "year": 1867
  },
  {
    "temp": 1.2,
    "lat": 71.042,
    "long": -0.594,
    "jday": 68.3,
    "year": 1867
  },
  {
    "temp": 0.6,
    "lat": 70.889,
    "long": -0.181,
    "jday": 68.67,
    "year": 1867
  },
  {
    "temp": 1.2,
    "lat": 70.578,
    "long": 1.431,
    "jday": 69.3,
    "year": 1867
  },
  {
    "temp": 1.9,
    "lat": 70.514,
    "long": 3.056,
    "jday": 70.3,
    "year": 1867
  },
  {
    "temp": 0.6,
    "lat": 70.642,
    "long": -1.803,
    "jday": 71.67,
    "year": 1867
  },
  {
    "temp": 0,
    "lat": 70.875,
    "long": -3.881,
    "jday": 72.3,
    "year": 1867
  },
  {
    "temp": -1.2,
    "lat": 70.917,
    "long": -4.506,
    "jday": 72.67,
    "year": 1867
  }
]
如果有人能告诉我我做错了什么就会很棒。 感谢

1 个答案:

答案 0 :(得分:0)

由于XMLHttpRequest是异步的,你应该在onload

中执行你的逻辑
request.onload = function() {
  if (request.status >= 200 && request.status < 400) {
    // Success!
    console.log(request[0].year);

  } else {
    // We reached our target server, but it returned an error

  }
};