Home  >  Article  >  Web Front-end  >  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

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

Linda Hamilton
Linda HamiltonOriginal
2024-11-01 13:34:02525browse

以下是几个根据你提供的文章内容生成的英文问答类标题:

1. Why does jQuery.parseJSON() throw an

jQuery.parseJSON throws "Invalid JSON" error due to escaped single quotes in JSON

jQuery.parseJSON() cannot parse JSON containing escaped single quotes Valid JSON string. This is normal because the JSON specification only allows escaping double quotes.

JSON Specification

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]

JSON Implementation

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

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 );
},

Conclusion

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!

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