Home >Web Front-end >JS Tutorial >How to Parse a JSON String with Single Quotes Instead of Double Quotes?
Parsing String as JSON with Single Quotes?
When attempting to parse a string as JSON, you may encounter issues if the string contains single quotes instead of the required double quotes.
Problem Statement:
Consider the example string str = {'a':1}, where single quotes are used around the object key. Parsing this string using JSON.parse(str) will result in the Uncaught SyntaxError: Unexpected token '…' exception.
Solution:
The JSON standard dictates that strings must be enclosed in double quotes. Parsing a string with single quotes will not be successful. To resolve this issue, there are two possible approaches:
JSON.parse(str.replace(/'/g, '"'));
The above is the detailed content of How to Parse a JSON String with Single Quotes Instead of Double Quotes?. For more information, please follow other related articles on the PHP Chinese website!