I'm using filereader API:
if (window.File && window.FileReader && window.FileList && window.Blob) {
} else {
alert('The File APIs are not fully supported in this browser.');
return;
}
input = document.id('fileinput');
if (!input) {
alert("Um, couldn't find the fileinput element.");
} else if (!input.files) {
alert("This browser doesn't seem to support the `files` property of file inputs.");
} else if (!input.files[0]) {
alert("Please select a file before clicking 'Load'");
} else {
file = input.files[0];
fr = new FileReader();
fr.onload = function () {
console.log(JSON.parse(fr.result));
}
fr.readAsText(file);
}
I'm getting this in my console:
{"id":"2","name":"Links1","position":"1","author":"Demo","email":"[email protected]","description":"","html":"<div class=\"units-row-end\" id=\"links\"><\/div>","css":"","js":"var out = '<a href=\"#!index\" class=\"home\">Home<\/a> ';\nout += user.type ? '<a href=\"#!logout\">Logout<\/a> ' : '<a href=\"#!login\">Login<\/a> ';\nout += user.type ==2 ? '<a href=\"#!admin?template=overview\">Account<\/a> ' : user.type == 1 ? '<a href=\"#!user?template=overview\">Account<\/a> ' : '';\nout += user.type !=2 ? '<a href=\"#!cart\">Cart<\/a>' : '';\n\ndocument.id('links').set('html', out);","status":"1"}
This output is not an object in my console! Why? If I'm trying to copy the string above and past into: http://json.parser.online.fr/ this string converts into object. Where is a problem?
P.S My file contents (fr.result):
"{\"id\":\"2\",\"name\":\"Links1\",\"position\":\"1\",\"author\":\"Demo\",\"email\":\"[email protected]\",\"description\":\"\",\"html\":\"<div class=\\\"units-row-end\\\" id=\\\"links\\\"><\\\/div>\",\"css\":\"\",\"js\":\"var out = '<a href=\\\"#!index\\\" class=\\\"home\\\">Home<\\\/a> ';\\nout += user.type ? '<a href=\\\"#!logout\\\">Logout<\\\/a> ' : '<a href=\\\"#!login\\\">Login<\\\/a> ';\\nout += user.type ==2 ? '<a href=\\\"#!admin?template=overview\\\">Account<\\\/a> ' : user.type == 1 ? '<a href=\\\"#!useraccount?template=overview\\\">Account<\\\/a> ' : '';\\nout += user.type !=2 ? '<a href=\\\"#!cart\\\">Cart<\\\/a>' : '';\\n\\ndocument.id('links').set('html', out);\",\"status\":\"1\"}"
JSON.parse(fr.result)is an object. Check it-console.log(typeof JSON.parse(fr.result))fr.resultlooks like and what console we're talking about (Firefox? Firebug? Chrome? Internet Explorer?). The rest of the code about FileReader API does not seem as relevant.