我已經創造了一些小JT代碼,但它給了我錯誤JSON解析錯誤:無法讀取屬性
function Mind(){
var request = "request";
var reply = "reply";
var words = '';
this.Reply = function(){
if(request == words.nouns[0].noun){
reply = words.nouns[0].noun;
}
else
reply = this.words.nouns[0].noun;
}
this.SetRequest = function(req){
request = req;
}
this.GetReply = function(){
return reply;
}
this.Parse = function(u){
var xmlhttp = new XMLHttpRequest();
var url = u;
var result;
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
words = JSON.parse(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
return result;
}
this.Construct = function(){
words = this.Parse('mind/words.json');
}}
var mind = new Mind();
mind.Parse('mind/words.json');
,這裏是我的JSON文件
{
"nouns": [
{"noun": "child"},
{"noun": "father"}
]
}
在命令生活一切順利的話,但是當我運行此代碼時,出現錯誤
Uncaught TypeError: Cannot read property 'nouns' of undefined
另見#1 FAQ:[訪問/處理對象,數組或JSON(http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Roberto