简体   繁体   中英

Unexpected token ' in JSON at position 0

When I print the str the console, this is my output (copied and pasted):

'{ "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

from the documentation on Mozilla and w3schools they specify that the string should take the form:

'{ "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

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.

I've tried removing the single space before the "1" and that still accomplishes nothing

As others have noted, you are appending a single-quote which result in

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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