首頁 > web前端 > js教程 > 主體

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

DDD
發布: 2024-10-20 13:08:29
原創
519 人瀏覽過

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中文網其他相關文章!

來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!