JSON
JSON
JSON
对象包含用于解析 JavaScript Object Notation (JSON
) 的方法,并将值转换为 JSON
。它不能被调用或者作为构造函数,除了它的两个方法属性,它本身并没有有趣的功能。
描述
JavaScript Object Notation
JSON是用于序列化对象,数组,数字,字符串,布尔值和等等的语法null
。它基于JavaScript语法,但不同于它:一些JavaScript不是JSON,有些JSON不是JavaScript。另请参阅JSON:不是的JavaScript子集。
JavaScript type | JSON differences |
---|---|
Objects and Arrays | Property names must be double-quoted strings; trailing commas are forbidden. |
Numbers | Leading zeros are prohibited( in JSON.stringify zeros will be ignored, but in JSON.parse it will throw SyntaxError a decimal point must be followed by at least one digit. |
Strings | Only a limited set of characters may be escaped; certain control characters are prohibited; the Unicode line separator (U+2028) and paragraph separator (U+2029) characters are permitted; strings must be double-quoted. See the following example where JSON.parse() works fine and a SyntaxError is thrown when evaluating the code as JavaScript: var code = '"\u2028\u2029"'; JSON.parse(code // works fine eval(code // fails |
完整的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
无效的空格可能存在于JSONNumber
(数字必须不包含空格)或JSONString
(其中它被解释为字符串中的相应字符,否则将导致错误)之外的任何位置。制表符(U+0009),回车(U+000D),换行(U+0020)和空格(U+0020)字符是唯一有效的空格字符。
方法
JSON.parse()
将字符串解析为JSON,可选地转换生成的值及其属性,然后返回值。JSON.stringify()
返回对应于指定值的JSON字符串,可选地只包含某些属性或以用户定义的方式替换属性值。
备注
JSON
对象不被旧版本浏览器支持。你可以把下面代码放到脚本的开始位置,这样就可以在那些没有原生支持 JSON
对象的浏览器(如IE6)中使用JSON
对象。
以下算法是对原生JSON
对象的模仿:
if (!window.JSON) {
window.JSON = {
parse: function(sJSON) { return eval('(' + sJSON + ')' },
stringify: (function () {
var toString = Object.prototype.toString;
var hasOwnProperty = Object.prototype.hasOwnProperty;
var isArray = Array.isArray || function (a) { return toString.call(a) === '[object Array]'; };
var escMap = {'"': '\\"', '\\': '\\\\', '\b': '\\b', '\f': '\\f', '\n': '\\n', '\r': '\\r', '\t': '\\t'};
var escFunc = function (m) { return escMap[m] || '\\u' + (m.charCodeAt(0) + 0x10000).toString(16).substr(1 };
var escRE = /[\\"\u0000-\u001F\u2028\u2029]/g;
return function stringify(value) {
if (value == null) {
return 'null';
} else if (typeof value === 'number') {
return isFinite(value) ? value.toString() : 'null';
} else if (typeof value === 'boolean') {
return value.toString(
} else if (typeof value === 'object') {
if (typeof value.toJSON === 'function') {
return stringify(value.toJSON()
} else if (isArray(value)) {
var res = '[';
for (var i = 0; i < value.length; i++)
res += (i ? ', ' : '') + stringify(value[i]
return res + ']';
} else if (toString.call(value) === '[object Object]') {
var tmp = [];
for (var k in value) {
// in case "hasOwnProperty" has been shadowed
if (hasOwnProperty.call(value, k))
tmp.push(stringify(k) + ': ' + stringify(value[k])
}
return '{' + tmp.join(', ') + '}';
}
}
return '"' + value.toString().replace(escRE, escFunc) + '"';
};
})()
};
}
众所周知,更复杂的JSON
对象 polyfills 是 JSON
2 和 JSON3
。
规范
Specification | Status | Comment |
---|---|---|
ECMAScript 5.1 (ECMA-262)The definition of 'JSON' in that specification. | Standard | Initial definition. |
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'JSON' in that specification. | Standard | |
ECMAScript Latest Draft (ECMA-262)The definition of 'JSON' in that specification. | Living Standard | |
浏览器兼容性
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic Support | (Yes) | (Yes) | 3.5 | 8 | 10.5 | 4 |
parse | (Yes) | (Yes) | 3.5 | 8 | 10.5 | 4 |
stringify | (Yes) | (Yes) | 3.5 | 8 | 10.5 | 4 |
Feature | Android | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic Support | (Yes) | (Yes) | (Yes) | 1 | (Yes) | (Yes) | (Yes) |
parse | (Yes) | (Yes) | (Yes) | 1 | (Yes) | (Yes) | (Yes) |
stringify | (Yes) | (Yes) | (Yes) | 1 | (Yes) | (Yes) | (Yes) |