I am trying to parse a JSON. I am using
$getJSON
to get the file and save it's content to a variable
JSONfile
, which I am then passing to the parsing function, but outside getJSON function it contains a null and inside it, it contains proper data even thought, the variable
JSONfile
is declared globally ( I think it is ). I am Javascript beginner. Please explain what is going on here or point me to something similar ( coulnd't find myself ).
var atlasJSON = "http://127.0.0.1:8000/sprites/SPRITE.json";
var JSONfile = null;
function setup(){
body = document.getElementById('body');
canvas = document.createElement('canvas');
spriteManager = new SpriteSheetClass();
spriteManager.load(spritesheet);
$.getJSON(atlasJSON, function(data) {
JSONfile = data;
console.log(JSONfile); // JSON file content is here
});
console.log(JSONfile); // null !!!
debugger;
spriteManager.parseAtlasDefinition(JSONfile);
for(var i=0; i<spriteManager.sprites.length ; i++){
console.log(spriteManager.sprites[i]);
}
//canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
canvas.setAttribute('width', 1024);
canvas.setAttribute('height',800);
body.appendChild(canvas);
};