I'm currently trying to load my JSON file into my JavaScript file.
I did a lot of research on trying to understand what I should be doing and how I should get it to work, but I can't get it to properly load into a variable.
This is the contents of my JSON file:
[
{
"id": 0,
"name": "Valley of Trials",
"desc": "Hello world",
"choices": []
},
{
"id": 1,
"name": "Valley of Trials",
"desc": "",
"choices": []
}
]
I'm loading and calling it like this:
var jsonLocations;
$.getJSON("../html/data.json", callbackFuncWithData);
function callbackFuncWithData(data) {
jsonLocations = data;
}
console.log(jsonLocations[0].name);
I read in a question on this site that it needed to be loaded this way otherwise the variable wouldn't be assigned the data due to the asynchronous calling. However, on opening the file in firefox (with firebug), I get an error saying: TypeError: jsonLocations is undefined.
However, in the watch list in firebug it gives me a rundown of all my variables, and I can open up jsonLocations and it will show me a tree hierarchy with the array, the objects and the properties.
So why is it that I'm getting this error? Surely if firebug is showing that jsonLocations has loaded my data it should be displayed in the console.log statement?
Please let me know what is wrong with my code.
Thanks, Joeb.