--- title: JSON slug: Web/JavaScript/Reference/Global_Objects/JSON tags: - JSON - JavaScript - NeedsTranslation - Object - Reference - Référence(2) - TopicStub - polyfill translation_of: Web/JavaScript/Reference/Global_Objects/JSON --- <div>{{JSRef}}</div> <p><strong><code>JSON</code></strong> 物件包含了解析、或是轉換為 <a class="external" href="http://json.org/">JavaScript Object Notation</a>({{glossary("JSON")}})格式的方法。這物件不能被呼叫或建構;而除了它的兩個方法屬性以外,本身也沒有特別的功能。</p> <h2 id="描述">描述</h2> <h3 id="JavaScript_Object_Notation">JavaScript Object Notation</h3> <p>JSON 是序列物件、陣列、數字、字串、布林值、還有 {{jsxref("null")}} 的語法。它建基、但不同於 JavaScript:有些 JavaScript 不是 JSON、而有些 JSON 不是 JavaScript。請參見 <a href="http://timelessrepo.com/json-isnt-a-javascript-subset">JSON: The JavaScript subset that isn't</a>。</p> <table> <caption>JavaScript 與 JSON 的差別</caption> <thead> <tr> <th scope="col">JavaScript 型別</th> <th scope="col">與 JSON 的差別</th> </tr> </thead> <tbody> <tr> <td>物件與陣列</td> <td>屬性名稱必須是包含在雙引號中的字串;禁止尾後逗號。</td> </tr> <tr> <td>數字</td> <td>數字不可以0作為開頭(在 JSON.stringify 0會被忽略,但是在 JSON.parse 會造成語法錯誤);小數點前面必須至少有一位數字。</td> </tr> <tr> <td>字串</td> <td> <p>Only a limited set of characters may be escaped; certain control characters are prohibited; the Unicode line separator (<a href="http://unicode-table.com/en/2028/">U+2028</a>) and paragraph separator (<a href="http://unicode-table.com/en/2029/">U+2029</a>) characters are permitted; strings must be double-quoted. See the following example where {{jsxref("JSON.parse()")}} works fine and a {{jsxref("SyntaxError")}} is thrown when evaluating the code as JavaScript:</p> <pre class="brush: js"> var code = '"\u2028\u2029"'; JSON.parse(code); // works fine eval(code); // fails </pre> </td> </tr> </tbody> </table> <p>JSON 的完整語法如下:</p> <pre><var>JSON</var> = <strong>null</strong> <em>or</em> <strong>true</strong> <em>or</em> <strong>false</strong> <em>or</em> <var>JSONNumber</var> <em>or</em> <var>JSONString</var> <em>or</em> <var>JSONObject</var> <em>or</em> <var>JSONArray</var> <var>JSONNumber</var> = <strong>-</strong> <var>PositiveNumber</var> <em>or</em> <var>PositiveNumber</var> <var>PositiveNumber</var> = DecimalNumber <em>or</em> <var>DecimalNumber</var> <strong>.</strong> <var>Digits</var> <em>or</em> <var>DecimalNumber</var> <strong>.</strong> <var>Digits</var> <var>ExponentPart</var> <em>or</em> <var>DecimalNumber</var> <var>ExponentPart</var> <var>DecimalNumber</var> = <strong>0</strong> <em>or</em> <var>OneToNine</var> <var>Digits</var> <var>ExponentPart</var> = <strong>e</strong> <var>Exponent</var> <em>or</em> <strong>E</strong> <var>Exponent</var> <var>Exponent</var> = <var>Digits</var> <em>or</em> <strong>+</strong> <var>Digits</var> <em>or</em> <strong>-</strong> <var>Digits</var> <var>Digits</var> = <var>Digit</var> <em>or</em> <var>Digits</var> <var>Digit</var> <var>Digit</var> = <strong>0</strong> through <strong>9</strong> <var>OneToNine</var> = <strong>1</strong> through <strong>9</strong> <var>JSONString</var> = <strong>""</strong> <em>or</em> <strong>"</strong> <var>StringCharacters</var> <strong>"</strong> <var>StringCharacters</var> = <var>StringCharacter</var> <em>or</em> <var>StringCharacters</var> <var>StringCharacter</var> <var>StringCharacter</var> = any character <em>except</em> <strong>"</strong> <em>or</em> <strong>\</strong> <em>or</em> U+0000 through U+001F <em>or</em> <var>EscapeSequence</var> <var>EscapeSequence</var> = <strong>\"</strong> <em>or</em> <strong>\/</strong> <em>or</em> <strong>\\</strong> <em>or</em> <strong>\b</strong> <em>or</em> <strong>\f</strong> <em>or</em> <strong>\n</strong> <em>or</em> <strong>\r</strong> <em>or</em> <strong>\t</strong> <em>or</em> <strong>\u</strong> <var>HexDigit</var> <var>HexDigit</var> <var>HexDigit</var> <var>HexDigit</var> <var>HexDigit</var> = <strong>0</strong> through <strong>9</strong> <em>or</em> <strong>A</strong> through <strong>F</strong> <em>or</em> <strong>a</strong> through <strong>f</strong> <var>JSONObject</var> = <strong>{</strong> <strong>}</strong> <em>or</em> <strong>{</strong> <var>Members</var> <strong>}</strong> <var>Members</var> = <var>JSONString</var> <strong>:</strong> <var>JSON</var> <em>or</em> <var>Members</var> <strong>,</strong> <var>JSONString</var> <strong>:</strong> <var>JSON</var> <var>JSONArray</var> = <strong>[</strong> <strong>]</strong> <em>or</em> <strong>[</strong> <var>ArrayElements</var> <strong>]</strong> <var>ArrayElements</var> = <var>JSON</var> <em>or</em> <var>ArrayElements</var> <strong>,</strong> <var>JSON</var> </pre> <p>Insignificant whitespace may be present anywhere except within a <code><var>JSONNumber</var></code> (numbers must contain no whitespace) or <code><var>JSONString</var></code> (where it is interpreted as the corresponding character in the string, or would cause an error). The tab character (<a href="http://unicode-table.com/en/0009/">U+0009</a>), carriage return (<a href="http://unicode-table.com/en/000D/">U+000D</a>), line feed (<a href="http://unicode-table.com/en/000A/">U+000A</a>), and space (<a href="http://unicode-table.com/en/0020/">U+0020</a>) characters are the only valid whitespace characters.</p> <h2 id="方法">方法</h2> <dl> <dt>{{jsxref("JSON.parse()")}}</dt> <dd>解析 JSON 字串,能改變給定值和屬性、接著回傳解析值。</dd> <dt>{{jsxref("JSON.stringify()")}}</dt> <dd>回傳給定的 JSON 對應字串,可自行決定只想包括哪些特定屬性、或替換的屬性值。</dd> </dl> <h2 id="Polyfill">Polyfill</h2> <p>舊版瀏覽器並不支援 <code>JSON</code>。你可以在程式碼開頭插入以下程式碼,以解決這個問題。這個程式碼能實做不支援原生 <code>JSON</code> 物件的瀏覽器(如 Internet Explorer 6)。</p> <p>以下演算法能仿製原生 <code>JSON</code> 物件。</p> <pre class="brush: js">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) + '"'; }; })() }; } </pre> <p>More complex well-known <a class="external" href="http://remysharp.com/2010/10/08/what-is-a-polyfill/">polyfills</a> for the <code>JSON</code> object are <a class="link-https" href="https://github.com/douglascrockford/JSON-js">JSON2</a> and <a class="external" href="http://bestiejs.github.com/json3">JSON3</a>.</p> <h2 id="規範">規範</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">Specification</th> <th scope="col">Status</th> <th scope="col">Comment</th> </tr> <tr> <td>{{SpecName('ES5.1', '#sec-15.12', 'JSON')}}</td> <td>{{Spec2('ES5.1')}}</td> <td>Initial definition.</td> </tr> <tr> <td>{{SpecName('ES6', '#sec-json-object', 'JSON')}}</td> <td>{{Spec2('ES6')}}</td> <td> </td> </tr> <tr> <td>{{SpecName('ESDraft', '#sec-json-object', 'JSON')}}</td> <td>{{Spec2('ESDraft')}}</td> <td> </td> </tr> </tbody> </table> <h2 id="瀏覽器相容性">瀏覽器相容性</h2> <div>{{Compat("javascript.builtins.JSON")}}</div> <h2 id="參見">參見</h2> <ul> <li>{{jsxref("Date.prototype.toJSON()")}}</li> </ul>