Home  >  Article  >  Web Front-end  >  Is Enclosing JSON Data in Single Quotes Valid with jQuery.parseJSON?

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

Barbara Streisand
Barbara StreisandOriginal
2024-10-20 13:05:30527browse

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>

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>

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!

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