2016-11-18 237 views
0

我想從我有JSON文件輸出數據,但我得到錯誤TypeError:無法讀取未定義的屬性'0'。這裏是我的腳本和JSON代碼...無法讀取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 
 
    } 
 
]
如果有人能告訴我什麼,我做錯了,這將是巨大的。 感謝

回答

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 

    } 
};