如何将 javascript 数组分配给外部 json 文件中的对象数组?
这是我尝试过的。
JavaScript 代码段
var i = 0;
var testjson = $.getJSON('/TestJSON');
jsonObj = JSON.parse(testjson);
$("#testJSONBtn").click(function () {
while (i <= jsonObj.events.length) {
$("#JSONOutput").append(jsonObj.events[i].title + ", " + jsonObj.events[i].date + ", " + jsonObj.events[i].explanation + "<br/>")
i += 1;
}
});
JSON 文件内容
{
"events":
[
{"title":"Okmulgee Public Schools Starts 3rd Quarter" , "date":"1-2-2013" , "explanation":"Okmulgee Public Schools begins its third quarter."}
{"title":"Okmulgee Public Schools-Closed in Observance of Martin Luther King Jr. Holiday" , "date":"1-21-2013" , "explanation":"The Okmulgee Public Schools will be closed in observance of the Martin Luther King Jr. holiday."}
{"title":"Okmulgee Public Schools County Professional Day" , "date":"2-1-2013" , "explanation":"Okmulgee Public Schools County Professional Day is today."}
]
}
我究竟做错了什么?