Single vs. Double Quotes in jQuery.parseJSON
When using jQuery's parseJSON method, users may encounter differences in behavior depending on whether single or double quotes are used to enclose the JSON string. In this article, we'll explore these differences.
Double Quotes: The Standard Approach
According to the JSON standard, double quotes are considered the preferred method for enclosing JSON strings. This is also the case with jQuery's parseJSON method, which expects JSON strings to be in double quotes. The following example illustrates this:
var obj1 = jQuery.parseJSON('{"orderedList": "true"}');
document.write("obj1 " obj1.orderedList); // Outputs "obj1 true"
Single Quotes: An Unsupported Format
In contrast, single quotes are not considered a valid JSON string format. As a result, using single quotes when calling parseJSON will lead to an error. The following example demonstrates this:
var obj2 = jQuery.parseJSON("{'orderedList': 'true'}");
document.write("obj2 " obj2.orderedList); // Outputs "obj2 undefined"
This behavior is not specific to jQuery. Rather, it is rooted in the JSON standard itself, which mandates the use of double quotes for string values. Therefore, regardless of the JavaScript toolkit being used, it is essential to use double quotes when working with JSON strings.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3