How do I assign a javascript array to that of an array of objects from an EXTERNAL json file?
Here's what I've tried.
JavaScript Snippet
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 File Contents
{
"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."}
]
}
What am I doing wrong?
$.getJSON
is an ajax call. – Snuffleupagus Jan 2 at 20:32