Я использую FileReader API:javascript | JSON синтаксического анализа
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);
}
Я получаю это в моей консоли:
{"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"}
Этот выход не является объектом в моей консоли! Зачем? Если я пытаюсь скопировать строку выше и прошёл в: http://json.parser.online.fr/, эта строка преобразуется в объект. Где проблема?
PS Моего содержимое файла (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)' это объект. Проверьте это: 'console.log (typeof JSON.parse (fr.result))' – karaxuna
Консоль показать мне: строка – user889349
Нам нужно знать, что такое 'fr.результат' выглядит так, и о какой консоли мы говорим (Firefox? Firebug? Chrome? Internet Explorer?). Остальная часть кода о API FileReader не кажется релевантной. –