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);
}
내 콘솔에 다음이 표시됩니다.
{"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. 결과) :
"{\"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\"}"
실제 파일 내용 ( fr.result
Firefox 콘솔로 덤프 하는 대신 텍스트 편집기로 파일을 열 때 표시됨 )이 정확히 다음과 같은 경우 :
"{\"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 유효성 검사기 가 공개하는 것처럼 실제로 유효한 JSON이 없습니다 . JSON 데이터 형식에서 최상위 요소는 객체 또는 배열이어야하며 귀하의 요소는 문자열입니다. 그러나 JSON.parse () 메서드는 분명히 이러한 JSON 하위 요소의 경우를 허용합니다 (아마도이를 디코딩 할 수 있다는 것이 유용하고 구현하기 쉬운 기능이기 때문일 것입니다).
그러나 하나의 문자열 만 가지고 있기 때문에 디코딩 후 얻는 것이 바로 문자열입니다. JSON에 관한 한 데이터 형식은 다음과 다르지 않습니다.
"Hello, World!"
JSON 파일 을 생성 하는 데 사용하는 코드에 문제가 있다고 생각 합니다. 출력을 두 번 인코딩하는 것 같습니다 . 아마도 다음과 같이 보일 것입니다.
{"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"}
선행 및 후행 따옴표가 사라졌습니다 (모든 백 슬래시 이스케이프 시퀀스와 함께). 이렇게하면 인코딩 된 개체가 있고 디코딩 된 개체가 생성됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다