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

Is Enclosing JSON Data in Single Quotes Valid with jQuery.parseJSON?

Barbara Streisand
Release: 2024-10-20 13:05:30
Original
529 people have browsed it

Is Enclosing JSON Data in Single Quotes Valid with jQuery.parseJSON?

Single vs. Double Quotes in jQuery.parseJSON

When utilizing jQuery.parseJSON to parse JSON data, it is crucial to understand the distinction between single and double quotes.

Let's examine two scenarios:

Scenario 1 (Works):

<code class="javascript">var obj1 = jQuery.parseJSON('{"orderedList": "true"}');
document.write("obj1 "+ obj1.orderedList );</code>
Copy after login

In this example, the JSON data is enclosed in double quotes.

Scenario 2 (Does Not Work):

<code class="javascript">var obj2 = jQuery.parseJSON("{'orderedList': 'true'}");
document.write("obj2 "+ obj2.orderedList );</code>
Copy after login

In this example, the JSON data is enclosed in single quotes.

The reason for the different behavior lies in the JSON specification. According to the JSON standard:

"A value can be a string in double quotes, or a number, or true or false or null, or an object or an array."

Therefore, double quotes are the standard while single quotes are not recognized. This applies to JSON in general, regardless of the parsing library being used.

In conclusion, it is essential to adhere to the JSON standard and enclose JSON data in double quotes when using jQuery.parseJSON. Failure to do so may result in parsing errors.

The above is the detailed content of Is Enclosing JSON Data in Single Quotes Valid with 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!