Home >Web Front-end >JS Tutorial >Why Does jQuery.parseJSON Throw an \'Invalid JSON\' Error When Escaped Single Quotes Are Present?
jQuery.parseJSON Error: "Invalid JSON" due to Escaped Single Quote
jQuery.parseJSON encounters difficulties parsing JSON objects with escaped single quotes ('). This behavior stems from the JSON specification's strict adherence to using double-quotes within strings.
According to the JSON state machine diagram, escaping or avoiding single quotes altogether is necessary. Single quotes are not valid string delimiters, making them unnecessary to escape.
Douglas Crockford, the architect of JSON, suggests that this restriction arose from JSON's minimalistic design. Fewer rules enhance interoperability by reducing the potential for compatibility issues. Using double-quotes exclusively eliminates the possibility of accidentally terminating a string with a single quote.
Despite this restriction, some JSON implementations, such as org.json, allow single quotes for greater permissiveness. These implementations interpret escaped single quotes in strings identically to escaped double quotes.
However, jQuery relies on the underlying native JSON parser or JSON library to validate JSON data. Unfortunately, these underlying tools are typically more restrictive and do not accept escaped single quotes. Consequently, jQuery cannot parse JSON data containing single quotes, resulting in the "Invalid JSON" error.
The above is the detailed content of Why Does jQuery.parseJSON Throw an \'Invalid JSON\' Error When Escaped Single Quotes Are Present?. For more information, please follow other related articles on the PHP Chinese website!