Home > Web Front-end > JS Tutorial > body text

Why Do Double Quotes and Single Quotes Act Differently in jQuery.parseJSON?

DDD
Release: 2024-10-20 13:08:29
Original
590 people have browsed it

Why Do Double Quotes and Single Quotes Act Differently in jQuery.parseJSON?

Single vs. Double Quotes in jQuery.parseJSON

In JavaScript, the jQuery.parseJSON method converts a JSON string into a JavaScript object. When working with JSON strings, it's crucial to understand the difference between using single and double quotes.

The Issue

The code snippet below works successfully:

var obj1 = jQuery.parseJSON('{"orderedList": "true"}');
Copy after login

However, the following code fails to parse the JSON string:

var obj2 = jQuery.parseJSON("{'orderedList': 'true'}");
Copy after login

The Reason

The discrepancy arises from the different handling of single and double quotes in JSON syntax. According to the JSON standard, double quotes are considered the standard for string delimiters, while single quotes are not.

JSON Syntax

The JSON syntax specifies that:

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.
Copy after login

Therefore, JavaScript can correctly parse double quotes when using them in a JSON string. Single quotes are not considered valid string delimiters, causing jQuery.parseJSON to fail to convert JSON strings correctly.

Update

It’s worth noting that a similar issue has come up in jQuery community discussions regarding the use of single quotes in JSON responses. One way to solve this problem is to ensure that you always use double quotes in the JSON string.

The above is the detailed content of Why Do Double Quotes and Single Quotes Act Differently in jQuery.parseJSON?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template