Home  >  Article  >  Web Front-end  >  Why Does jQuery.parseJSON Fail on JSON Strings with Escaped Single Quotes?

Why Does jQuery.parseJSON Fail on JSON Strings with Escaped Single Quotes?

Susan Sarandon
Susan SarandonOriginal
2024-10-28 06:14:30906browse

Why Does jQuery.parseJSON Fail on JSON Strings with Escaped Single Quotes?

jQuery.parseJSON Error: Invalid JSON Due to Escaped Single Quotes

Problem Statement

jQuery.parseJSON fails to parse JSON strings containing escaped single quotes ('), leading to an "Invalid JSON" error. This issue arises when JSON data includes values with single quotes that are properly escaped.

Answer

The official JSON specification allows only double quotes to enclose strings, not single quotes. Escaping single quotes is not supported in the formal JSON grammar.

This design decision simplifies JSON parsing and prevents accidental string termination by single quotes. As a result, jQuery.parseJSON relies on the underlying JSON parser implementations, which typically adhere to this specification and do not accept single quotes.

Additional Information

  • Douglas Crockford, the creator of JSON, emphasizes the principle of minimalism and interoperability, which influenced the exclusion of escaped single quotes.
  • Some Java-based JSON implementations, such as org.json, may allow single quotes, but relying on this is not recommended.
  • The browser's native JSON parsers also follow the standard and do not accept escaped single quotes.
  • jQuery.parseJSON uses these native parsers or a JSON library (json2.js) if available, making it subject to their restrictions on single quotes.

The above is the detailed content of Why Does jQuery.parseJSON Fail on JSON Strings with Escaped Single Quotes?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn