Home >Web Front-end >JS Tutorial >The following are several English question and answer titles generated based on the article content you provided: 1. Why does jQuery.parseJSON() throw an \'Invalid JSON\' error when there are escaped single quotes in the JSON? 2. Is it allowed to escape single quotes in a JSO
jQuery.parseJSON() cannot parse JSON containing escaped single quotes Valid JSON string. This is normal because the JSON specification only allows escaping double quotes.
As shown below, the JSON state machine diagram indicates that only double quotes are allowed to be escaped, single quotes are not allowed.
[Image: JSON State Machine]
Although the specification does not allow escaping single quotes, some JSON implementations may accept them. For example, org.json for Java allows single quotes, while json2.js used by jQuery follows the specification and does not allow them.
jQuery.parseJSON first attempts to use the browser's native JSON parser or json2.js, so it is only as permissive as the underlying implementation. Since json2.js follows the specification, jQuery also does not allow single quotes.
parseJSON: function( data ) { ... // Attempt to parse using the native JSON parser first if ( window.JSON && window.JSON.parse ) { return window.JSON.parse( data ); } ... jQuery.error( "Invalid JSON: " + data ); },
To avoid errors when parsing JSON using jQuery.parse, avoid using single quotes in JSON strings, or switch to a JSON library that accepts single quote implementations.
The above is the detailed content of The following are several English question and answer titles generated based on the article content you provided: 1. Why does jQuery.parseJSON() throw an \'Invalid JSON\' error when there are escaped single quotes in the JSON? 2. Is it allowed to escape single quotes in a JSO. For more information, please follow other related articles on the PHP Chinese website!