JSON 对象包含的方法用于剖析 JavaScript 对象表示法 ( JSON ) and converting values to JSON. It can't be called or constructed, and aside from its two method properties, it has no interesting functionality of its own.

描述

JavaScript 和 JSON 的差异

JSON is a syntax for serializing objects, arrays, numbers, strings, booleans, and null . It is based upon JavaScript syntax but is distinct from it: some JavaScript is not JSON.

对象和数组
Property names must be double-quoted strings; trailing commas are forbidden.
数字
Leading zeros are prohibited. A decimal point must be followed by at least one digit. NaN and Infinity are unsupported.
任何 JSON 文本都是有效 JavaScript 表达式 ...
...But only in JavaScript engines that have implemented the proposal to make all JSON text valid ECMA-262 . In engines that haven't implemented the proposal, U+2028 LINE SEPARATOR and U+2029 PARAGRAPH SEPARATOR are allowed in string literals and property keys in JSON; but their use in these features in JavaScript string literals is a SyntaxError .

Consider this example where JSON.parse() parses the string as JSON and eval executes the string as JavaScript:

let code = '"\u2028\u2029"'
JSON.parse(code)  // evaluates to "\u2028\u2029" in all engines
eval(code)        // throws a SyntaxError in old engines
					

Other differences include allowing only double-quoted strings and having no provisions for undefined or comments. For those who wish to use a more human-friendly configuration format based on JSON, there is JSON5 , used by the Babel compiler, and the more commonly used YAML .

完整 JSON 句法

完整 JSON 语法如下:

JSON = null
    or true or false
    or JSONNumber
    or JSONString
    or JSONObject
    or JSONArray
JSONNumber = - PositiveNumber
          or PositiveNumber
PositiveNumber = DecimalNumber
              or DecimalNumber . Digits
              or DecimalNumber . Digits ExponentPart
              or DecimalNumber ExponentPart
DecimalNumber = 0
             or OneToNine Digits
ExponentPart = e Exponent
            or E Exponent
Exponent = Digits
        or + Digits
        or - Digits
Digits = Digit
      or Digits Digit
Digit = 0 through 9
OneToNine = 1 through 9
JSONString = ""
          or " StringCharacters "
StringCharacters = StringCharacter
                or StringCharacters StringCharacter
StringCharacter = any character
                  except " or \ or U+0000 through U+001F
               or EscapeSequence
EscapeSequence = \" or \/ or \\ or \b or \f or \n or \r or \t
              or \u HexDigit HexDigit HexDigit HexDigit
HexDigit = 0 through 9
        or A through F
        or a through f
JSONObject = { }
          or { Members }
Members = JSONString : JSON
       or Members , JSONString : JSON
JSONArray = [ ]
         or [ ArrayElements ]
ArrayElements = JSON
             or ArrayElements , JSON
					

Insignificant whitespace may be present anywhere except within a JSONNumber (numbers must contain no whitespace) or JSONString (where it is interpreted as the corresponding character in the string, or would cause an error). The tab character ( U+0009 ), carriage return ( U+000D ), line feed ( U+000A ), and space ( U+0020 ) characters are the only valid whitespace characters.

静态方法

JSON.parse( text [, reviver ])
剖析字符串 text as JSON, optionally transform the produced value and its properties, and return the value. Any violations of the JSON syntax, including those pertaining to the differences between JavaScript and JSON, cause a SyntaxError to be thrown. The reviver option allows for interpreting what the replacer has used to stand in for other datatypes.
JSON.stringify( value [, replacer [, space ]])
Return a JSON string corresponding to the specified value, optionally including only certain properties or replacing property values in a user-defined manner. By default, all instances of undefined are replaced with null , and other unsupported native data types are censored. The replacer option allows for specifying other behavior.

范例

JSON 范例

{
  "browsers": {
    "firefox": {
      "name": "Firefox",
      "pref_url": "about:config",
      "releases": {
        "1": {
          "release_date": "2004-11-09",
          "status": "retired",
          "engine": "Gecko",
          "engine_version": "1.7"
        }
      }
    }
  }
}
					

规范

规范
ECMAScript (ECMA-262)
在该规范中的 JSON 定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
JSON Chrome 3 Edge 12 Firefox 3.5 IE 8 Opera 10.5 Safari 4 WebView Android ≤37 Chrome Android 18 Firefox Android 4 Opera Android 11 Safari iOS 4 Samsung Internet Android 1.0 nodejs 0.1.100
JavaScript is a superset of JSON Chrome 66 Edge 79 Firefox 62 IE No Opera 53 Safari 12 WebView Android 66 Chrome Android 66 Firefox Android 62 Opera Android 47 Safari iOS 12 Samsung Internet Android 9.0 nodejs 10.0.0
parse Chrome 3 Edge 12 Firefox 3.5 IE 8 Opera 10.5 Safari 4 WebView Android ≤37 Chrome Android 18 Firefox Android 4 Opera Android 11 Safari iOS 4 Samsung Internet Android 1.0 nodejs 0.1.100
stringify Chrome 3 Edge 12 Firefox 3.5 IE 8 Opera 10.5 Safari 4 WebView Android ≤37 Chrome Android 18 Firefox Android 4 Opera Android 11 Safari iOS 4 Samsung Internet Android 1.0 nodejs 0.1.100

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: