I'm trying to get access to a JSON object which is selected locally using the HTML 5 API. My code so far is:
HTML
<input type="file" id="files" name="files[]" />
Javascript
JsonObj = null
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
f = files[0];
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function (theFile) {
return function (e) {
// Render thumbnail.
JsonObj = e.target.result
console.log(JsonObj);
var parsedJSON = JSON.parse(JsonObj);
var x = parsedJSON['frames']['chaingun.png']['spriteSourceSize']['x'];
console.log(x);
};
})(f);
// Read in JSON as a data URL.
reader.readAsDataURL(f);
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
Fiddle here: http://jsfiddle.net/jamiefearon/8kUYj/11/
So the user selects a Json file which is then represented by JsonObj
. To test I have assess to the Json object I print to console: parsedJSON['frames']['chaingun.png']['spriteSourceSize']
I get the following error in Chrome in the console:
fiddle.jshell.net:33Uncaught SyntaxError: Unexpected token ILLEGAL
(anonymous function)fiddle.jshell.net:33
For reference the Json file I am using is:
JSONExample = {
"frames": {
"chaingun.png": {
"frame": {
"x": 1766,
"y": 202,
"w": 42,
"h": 34
},
"rotated": false,
"trimmed": true,
"spriteSourceSize": {
"x": 38,
"y": 32,
"w": 42,
"h": 34
},
"sourceSize": {
"w": 128,
"h": 128
}
}
}
};
Thank you all for your help.