简体   繁体   English

JSON 中的意外令牌 ' 在 position 0

[英]Unexpected token ' in JSON at position 0

When I print the str the console, this is my output (copied and pasted):当我在控制台打印 str 时,这是我的 output (复制并粘贴):

'{ "1":"574fdd158e8f6581.jpg", "2":"b008442edfd5f9c5.jpg", "3":"14a88efa3eba4329.jpg", "4":"270ee8798e00032f.jpg", "5":"6b440c1ccc321e3a.jpg", "6":"272244601e225be9.jpg"}'

when I print out the typeof str it correctly identifies it as a string当我打印出 typeof str 时,它正确地将其标识为字符串

from the documentation on Mozilla and w3schools they specify that the string should take the form:从 Mozilla 和 w3schools 的文档中,他们指定字符串应采用以下形式:

'{ "name":"John", "age":30, "city":"New York"}'

Yet when I try to do JSON.parse(str) I'm getting this error from the console: Uncaught SyntaxError: Unexpected token ' in JSON at position 0然而,当我尝试执行 JSON.parse(str) 时,我从控制台收到此错误: Uncaught SyntaxError: Unexpected token ' in JSON at Z4757FE07FD492A8BE60EA6A760D683

even the error being thrown seems to imply that the error is that the character at position 0 is the character that's supposed to be at position 0. I'm not quite sure why this isn't working properly.即使抛出的错误似乎也暗示错误是 position 0 处的字符应该是 position 0 处的字符。我不太确定为什么这不能正常工作。

I've tried removing the single space before the "1" and that still accomplishes nothing我试过删除“1”之前的单个空格,但仍然没有任何效果

As others have noted, you are appending a single-quote which result in正如其他人所指出的,您正在附加一个单引号,导致

Uncaught SyntaxError: Unexpected token ' in JSON at position 0 Uncaught SyntaxError: Unexpected token ' in JSON at position 0

Example:例子:

 const validJSON = `{ "name":"John", "age":30, "city":"New York"}`; const invalidJSON = `'{ "name":"John", "age":30, "city":"New York"}'`; console.log({validJSON: JSON.parse(validJSON)}); console.log({invalidJSON: JSON.parse(invalidJSON)});

Open the console to view the error.打开控制台查看错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM