首頁  >  文章  >  web前端  >  為什麼雙引號和單引號在 jQuery.parseJSON 中的行為不同?

為什麼雙引號和單引號在 jQuery.parseJSON 中的行為不同?

DDD
DDD原創
2024-10-20 13:08:29525瀏覽

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 str. When working with JSON strings, it's crucial to understand the difference between using single and double quotes.

The Issue

The Issue

var obj1 = jQuery.parseJSON('{"orderedList": "true"}');
However, the following code fails to parse the JSON string:

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

The Reason

The discreation orhing 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 SyntaxTheoA; syntax specifies that:

因此,在JSON 字串中使用雙引號時,JavaScript 才能正確解析它。單引號不被視為有效的字串定界符,導致 jQuery.parseJSON 無法正確轉換 JSON 字串。

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.
更新

值得注意的是,類似的問題曾在 jQuery 社群的討論中出現過,即在 JSON 回應中使用單引號。解決這個問題的一種方法是確保在 JSON 字串中始終使用雙引號。

以上是為什麼雙引號和單引號在 jQuery.parseJSON 中的行為不同?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn