mozilla

版本 405653 / JSON

  • 版本网址缩略名: JSON
  • 版本标题: JSON
  • 版本 id: 405653
  • 创建于:
  • 创建者: Will_Chen
  • 是否是当前版本?
  • 评论

修订内容

JSON (JavaScript Object Notation) 是一个数据交换格式。它非常的接近于JavaScript语法的子集,尽管它不是一个严格的子集。(详细见JavaScript参考中的JSON。)当你在写任何基于JavaScript的应用程序的时候它都是非常有用的,包括网站和浏览器拓展。举个例子,也许你会把用户信息储存成JSON格式并放到cookie中,或者你会把拓展偏好(extension preferences)储存成为JSON格式在一个字符串值(string-valued)的浏览器偏好(browser preference)中。

JSON 的才能是由数、布尔,字符串,null以及数组(按照值的顺序排列)甚至对象(string-value mappings)这些值的表示来组成的(或者其他的数组或对象)。它不能与生俱来的用来表示更负载的数据类型,比如函数、正则表达式,日期等等。(日期 对象默认会被序列化成为一个ISO格式的包含日期的字符串,因此它们不往返行程(round-trip),这个信息不会完全丢失。)如果你需要存储这些 值,你可以在它们被序列化的时候把它们变成值,或者先把他们反序列化(deserialization),去使JSON可以表现这些后加的数据类型。

文档

JSON对象(JSON object)
JSON对象包含一些把值转换成JavaScript Object Notation (JSON)和一些转换JSON成为值的方法(methods)。
使用本地JSON(Using native JSON
这个文章覆盖了在Gecko 1.9.1被添加的ECMAScript 5遵从本地JSON对象。
JSONPath
JSONPath是使用类似XPath语法去查询JSON结构。它不是一个标准,但是它确实代表了在不需要转换或者从XML的前提下直接查询JavaScript结构。
Downloading JSON and JavaScript in extensions
A common practice found in many extensions is using XMLHttpRequest (or some other mechanism) to download JavaScript or JSON (they are different) from a remote website. Once the content has been downloaded, the extension authors proceed to use eval() to decode the string content into JavaScript objects, which is a dangerous practice and will not, in fact, pass an AMO review. This article shows how to avoid it.
JXON
JXON 代表无损的 Javascript XML Object Notation,它是一个用来定义使用xml表示JavaScript对象树的署名。
JSON.stringify method
Convert a value to JSON, optionally replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified.
JSON.parse method
把字符串解析成为JSON,选择性的把值转换成JSON。

查看全部...

社区

  • 查看Mozilla论坛...

{{ DiscussionList("dev-tech-json", "mozilla.dev.tech.json") }}

工具

查看全部...

AJAX, JavaScript, XUL

通过使用JavaScript的JSON 对象使用JSON

最简单的使用JSON的办法是通过标准JavaScript中的JSON 对象来使用。使用JSON.stringify去序列化一个JSON值,以及使用JSON.parse去反序列化一个JSON值。

例子,使用JSON.stringify()方法去序列化一个JavaScript对象:

var foo = {};
foo.bar = "new property";
foo.baz = 3;

var JSONfoo = JSON.stringify(foo);

JSONfoo 现在变成了 {"bar":"new property","baz":3}. 如果你想使 JSONfoo 再变回一个 JavaScript 对象,你只需要做:

var backToJS = JSON.parse(JSONfoo);

更多的信息你可以看JSON对象文档

toJSON()方法

如果一个对象定义了一个toJSON()方法,JSON.stringify会调用这个方法去终止这个JSON对象的转换,例子:

x = {}; 
x.foo = "foo";  
x.toJSON = function() { return "bar"; }; 
var json1 = JSON.stringify(x); 

json1 现在的结果是 '"bar"'。

译者注(下图是我在Chrome Console下做的):

限制

你不能序列化一个含有functions的对象,JSON规定这是不被允许的。例子:

foo.qwerty = function(){alert('foobar');}; 
foo.qwerty() 
var JSONfoo = JSON.toString(foo); 

它会抛出: TypeError on line XXX: No JSON representation for this object!

通过nsIJSON使用JSON

3.5版本之前的Firefox并不原生支持JSON,所以唯一的办法是使用nsIJSON组件,从Firefox 3开始生效。使用nsIJSONencodedecode方法来编解码。这些方法对于JSON对象来说并不足够强大,他们只支持那些是对象或者是数组的值,并不支持字符串,布尔,数字或者null。(这些值仅仅会被变成数组或对象的时候才能被支持:数字5是不会被支持的,但是{ "value": 5 }可以。

因为nsIJSON对于JSON对象来说,并不足够强大,它的encodedecode方法从Firefox 7起已经被移除了。开发需要选择使用JSON对象来代替。

下面的代码是使用nsIJSON序列化一个JavaScript对象:

var Ci = Components.interfaces;
var Cc = Components.classes;

var foo = {};
foo.bar = "new property";
foo.baz = 3;

var nativeJSON = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
var JSONfoo = nativeJSON.encode(foo);

现在 JSONfoo 变量变成了字符串{"bar":"new property","baz":3}。JSONfoo再变成JavsScript对象,只需要做:

var backToJS = nativeJSON.decode(JSONfoo);

其他使用JSON的方法

理论上讲,是可以用eval去使用JSON的(但仅仅是反序列化一个对象,而不是把一个对象序列化成为一个字符串)或者是使用json2.js,不是一个好办法。用eval去解析JSON是不安全的,因为eval允许比JSON更多的语法(会使代码随意的执行)。对于json2.js,它的问题是它在构造对象的时候向其中添加了一些方法,比如Object.prototype,它非常有可能按照某种不确定的方式破坏代码。你几乎永远都需要使用先前方法中的一个去用来表述JSON。

还可以看

  • {{ interface("nsIJSON") }}

{{ languages( { "ja": "ja/JSON", "zh-cn": "zh-cn/JSON" } ) }}

修订版来源

<p><strong>JSON</strong> (<strong>JavaScript Object Notation</strong>) 是一个数据交换格式。它非常的接近于<a href="/en/JavaScript" title="en/JavaScript">JavaScript</a>语法的子集,尽管它不是一个严格的子集。(详细见<a href="/en/JavaScript/Reference" title="en/JavaScript/Reference">JavaScript参考</a>中的<a href="/en/JavaScript/Reference/Global_Objects/JSON" title="en/JavaScript/Reference/Global Objects/JSON">JSON</a>。)当你在写任何基于JavaScript的应用程序的时候它都是非常有用的,包括网站和浏览器拓展。举个例子,也许你会把用户信息储存成JSON格式并放到<a href="/en/DOM/document.cookie" title="en/document.cookie">cookie</a>中,或者你会把拓展偏好(extension preferences)储存成为JSON格式在一个字符串值(string-valued)的浏览器偏好(browser preference)中。</p>
<p>JSON 的才能是由数、布尔,字符串,null以及数组(按照值的顺序排列)甚至对象(string-value mappings)这些值的表示来组成的(或者其他的数组或对象)。它不能与生俱来的用来表示更负载的数据类型,比如函数、正则表达式,日期等等。(日期 对象默认会被序列化成为一个ISO格式的包含日期的字符串,因此它们不往返行程(round-trip),这个信息不会完全丢失。)如果你需要存储这些 值,你可以在它们被序列化的时候把它们变成值,或者先把他们反序列化(deserialization),去使JSON可以表现这些后加的数据类型。</p>
<table class="topicpage-table">
  <tbody>
    <tr>
      <td>
        <h4 id="Documentation" name="Documentation"><a href="/Special:Tags?tag=JSON&amp;language=en" title="Pages tagged with: JSON">文档</a></h4>
        <dl>
          <dt>
            <a href="/en/JavaScript/Reference/Global_Objects/JSON" title="en/JavaScript/Reference/Global_Objects/JSON"><code>JSON对象(JSON</code> object)</a></dt>
          <dd>
            <small><code>JSON</code>对象包含一些把值转换成</small><small><a class="external" href="http://json.org/">JavaScript Object Notation</a> (JSON)和一些转换JSON成为值的方法(methods)。</small></dd>
        </dl>
        <dl>
          <dt>
            <a href="/en/Using_native_JSON" title="en/Using_native_JSON">使用本地JSON(Using native JSO</a><a href="/en/Using_native_JSON" title="en/Using_native_JSON">N</a><a href="/en/Using_native_JSON" title="en/Using_native_JSON">)</a></dt>
          <dd>
            这个文章覆盖了在<small>Gecko 1.9.1</small>被添加的<small>ECMAScript 5</small>遵从本地JSON对象。</dd>
        </dl>
        <dl>
          <dt>
            <a href="/en/JSON/JSONPath" title="en/JSON/JSONPath">JSONPath</a></dt>
          <dd>
            <small>JSONPath是使用</small>类似XPath语法去查询JSON结构。它不是一个标准,但是它确实代表了在不需要转换或者从XML的前提下直接查询JavaScript结构。</dd>
        </dl>
        <dl>
          <dt>
            <a href="/en/Downloading_JSON_and_JavaScript_in_extensions" title="en/Downloading_JSON_and_JavaScript_in_extensions">Downloading JSON and JavaScript in extensions</a></dt>
          <dd>
            <small>A common practice found in many extensions is using <a href="/en/DOM/XMLHttpRequest" title="en/DOM/XMLHttpRequest">XMLHttpRequest</a> (or some other mechanism) to download JavaScript or <a href="/en/JSON" title="en/JSON">JSON</a> (they are different) from a remote website. Once the content has been downloaded, the extension authors proceed to use <code><a href="/en/Core_JavaScript_1.5_Reference/Functions/eval" title="en/Core_JavaScript_1.5_Reference/Functions/eval"> eval()</a></code> to decode the string content into JavaScript objects, which is a <strong>dangerous</strong> practice and will not, in fact, pass an <a class="external" href="http://addons.mozilla.org">AMO</a> review. This article shows how to avoid it.</small></dd>
        </dl>
        <dl>
          <dt>
            <a href="/en/JXON" title="en/JXON">JXON</a></dt>
          <dd>
            <small>JXON 代表无损的 <strong>J</strong>avascript <strong>X</strong>ML <strong>O</strong>bject <strong>N</strong>otation,它是一个用来定义使用xml表示JavaScript对象树的署名。</small></dd>
        </dl>
        <dl>
          <dt>
            <a href="/en/JavaScript/Reference/Global_Objects/JSON/stringify" title="en/JavaScript/Reference/Global_Objects/JSON/stringify"><code>JSON.stringify</code> method</a></dt>
          <dd>
            <small>Convert a value to JSON, </small><small>optionally</small><small> replacing values if a replacer function is specified, or optionally including only the specified properties if a replacer array is specified.</small></dd>
        </dl>
        <dl>
          <dt>
            <a href="/en/JavaScript/Reference/Global_Objects/JSON/parse" title="en/JavaScript/Reference/Global_Objects/JSON/parse"><code>JSON.parse</code> method</a></dt>
          <dd>
            把字符串解析成为JSON,选择性的把值转换成JSON。</dd>
        </dl>
        <p><span class="alllinks"><a href="/Special:Tags?tag=JSON&amp;language=en" title="Pages tagged with: JSON">查看全部...</a></span></p>
      </td>
      <td>
        <h4 id="Community" name="Community">社区</h4>
        <ul>
          <li>查看Mozilla论坛...</li>
        </ul>
        <p>{{ DiscussionList("dev-tech-json", "mozilla.dev.tech.json") }}</p>
        <h4 id="Tools" name="Tools">工具</h4>
        <ul>
          <li><a class="internal" href="/en/JavaScript/Reference/Global_Objects/JSON#Browser_compatibility" title="JSON object polyfills">JSON polyfills</a></li>
          <li><a class="external" href="http://jsonselect.org" title="http://jsonselect.org/">JSONSelect</a></li>
        </ul>
        <p><span class="alllinks"><a href="/Special:Tags?tag=JSON:Tools&amp;language=en" title="Pages tagged with: JSON:Tools">查看全部...</a></span></p>
        <h4 id="Related_Topics" name="Related_Topics">相关话题</h4>
        <dl>
          <dd>
            <a href="/en/AJAX" title="en/AJAX">AJAX</a>, <a href="/en/JavaScript" title="en/JavaScript">JavaScript</a>, <a href="/en/XUL" title="en/XUL">XUL</a></dd>
        </dl>
      </td>
    </tr>
  </tbody>
</table>
<h2 id="JSON_in_Firefox_2" name="JSON_in_Firefox_2">通过使用JavaScript的JSON 对象使用JSON</h2>
<p>最简单的使用JSON的办法是通过标准JavaScript中的<a href="/en/JavaScript/Reference/Global_Objects/JSON" title="en/JavaScript/Reference/Global Objects/JSON">JSON 对象</a>来使用。使用<a href="/en/JavaScript/Reference/Global_Objects/JSON/stringify" title="en/JavaScript/Reference/Global Objects/JSON/stringify">JSON.stringify</a>去序列化一个JSON值,以及使用<a href="/en/JavaScript/Reference/Global_Objects/JSON/parse" title="en/JavaScript/Reference/Global Objects/JSON/parse">JSON.parse</a>去反序列化一个JSON值。</p>
<p>例子,使用<code>JSON.stringify()方法去序列化一个</code>JavaScript对象:</p>
<pre class="brush: js">
var foo = {};
foo.bar = "new property";
foo.baz = 3;

var JSONfoo = JSON.stringify(foo);
</pre>
<p><code>JSONfoo</code> 现在变成了 <code>{"bar":"new property","baz":3}</code>. 如果你想使 <code>JSONfoo</code> 再变回一个 JavaScript 对象,你只需要做:</p>
<pre class="brush: js">
var backToJS = JSON.parse(JSONfoo);
</pre>
<p>更多的信息你可以看<a href="/en/JavaScript/Reference/Global_Objects/JSON" title="en/JavaScript/Reference/Global Objects/JSON">JSON对象</a>文档</p>
<h2 id="toJSON().E6.96.B9.E6.B3.95">toJSON()方法</h2>
<p>如果一个对象定义了一个toJSON()方法,JSON.stringify会调用这个方法去终止这个JSON对象的转换,例子:</p>
<pre class="brush: js">
x = {}; 
x.foo = "foo"; &nbsp;
x.toJSON = function() { return "bar"; }; 
var json1 = JSON.stringify(x); 
</pre>
<p><code>json1</code> 现在的结果是 '"bar"'。</p>
<p>译者注(下图是我在Chrome Console下做的):</p>
<p><img alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAU0AAAGxCAIAAABpwnPxAAAgAElEQVR4nO2dW2/kSJbf8zsUoAd9BsMPfvCT4a/BB2MNz25BW68NOA0Y2ZjJmVwUCxiMmBIzUyvbKAq2ynypEoRJA1O7Y+0NmibskofeghoYT1seDLAwJ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ZHbNy9XbzhbeHux4sx18i2Z2AZEoHXOMiffkoklBLayCAGARIs6TwUspKrTZE74pDHSyrmG6aKyypNzUcP3b5++fHtLJ7aBPDkXNZy45WRiITDnoBJNdZ6pODfiopDlccjLWPKuWVaIEjqdmYYa/h+/kibn715n0+/bNy8TX51MTJtvoKmrUJqcXx1k0+/7873EVycTC4DMQTUarsNxnnc64ARnXBmFklrz5Tbe11Zv1IkVl41taviLDjljLLbwyYw91zOZyJrqXFk/iy18MmPP9UwmasEiHKhIz/tkNr91XgNyrb29mfYGVJtp1wUyB1XpQ+e8he/FzRR2yNy+eancIeuP+/M96bZZK2CHDKgF9rcDYD7QOQDmg/dMAGA+sOcAmA90DoD5QOcAmA90DoD5QOcAmA+h899fff6d1d/13xUAQEcQOv8Xy8//wfizPzj6ov/eAAC6gPbbv7P8zT+a/OYP/+2XPfcGANAF2vn5Hx7/v386+/vxf/xdn70BAHSBVue//8ef/5Pvf/nBv/+qz94AALqA1vnvHX76jyd/t/fHv+m5NwCALiB0/s/m/+cfjj/9jvtZ/70BAHQBofN/fnD/ewf/t/+uAAA6AvtkADAf6BwA8/n/04bBH6DI1pgAAAAASUVORK5CYII=" /></p>
<h2 id=".E9.99.90.E5.88.B6">限制</h2>
<p>你不能序列化一个含有functions的对象,JSON规定这是不被允许的。例子:</p>
<pre class="brush: js">
foo.qwerty = function(){alert('foobar');}; 
foo.qwerty() 
var JSONfoo = JSON.toString(foo); 
</pre>
<p>它会抛出: <code>TypeError on line XXX: No JSON representation for this object!</code></p>
<h2 id=".E9.80.9A.E8.BF.87nsIJSON.E4.BD.BF.E7.94.A8JSON">通过nsIJSON使用JSON</h2>
<p>3.5版本之前的Firefox并不原生支持JSON,所以唯一的办法是使用<code><a href="/en/XPCOM_Interface_Reference/nsIJSON" title="en/nsIJSON">nsIJSON</a></code>组件,从Firefox 3开始生效。使用<code>nsIJSON</code>的<code>encode</code> 和 <code>decode方法来编解码。这些方法对于</code>JSON对象来说并不足够强大,他们只支持那些是对象或者是数组的值,并不支持字符串,布尔,数字或者null。(这些值仅仅会被变成数组或对象的时候才能被支持:数字5是不会被支持的,但是<code>{ "value": 5 }可以。</code><code>)</code></p>
<p>因为nsIJSON对于JSON对象来说,并不足够强大,它的<code>encode</code> 和 <code>decode</code>方法从Firefox 7起已经被移除了。开发需要选择使用JSON对象来代替。</p>
<p>下面的代码是使用nsIJSON序列化一个JavaScript对象:</p>
<pre class="brush: js">
var Ci = Components.interfaces;
var Cc = Components.classes;

var foo = {};
foo.bar = "new property";
foo.baz = 3;

var nativeJSON = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
var JSONfoo = nativeJSON.encode(foo);
</pre>
<p>现在 <code>JSONfoo</code> 变量变成了字符串<code>{"bar":"new property","baz":3}。</code> 把 <code>JSONfoo</code>再变成JavsScript对象,只需要做:</p>
<pre class="brush: js">
var backToJS = nativeJSON.decode(JSONfoo);
</pre>
<h2 id="Using_JSON" name="Using_JSON">其他使用JSON的方法</h2>
<p>理论上讲,是可以用<a href="/en/JavaScript/Reference/Global_Objects/eval" title="en/JavaScript/Reference/Global Objects/eval">eval</a>去使用JSON的(但仅仅是反序列化一个对象,而不是把一个对象序列化成为一个字符串)或者是使用<a class="link-https" href="https://github.com/douglascrockford/JSON-js" title="https://github.com/douglascrockford/JSON-js">json2.js</a>,不是一个好办法。用eval去解析JSON是不安全的,因为eval允许比JSON更多的语法(会使代码随意的执行)。对于json2.js,它的问题是它在构造对象的时候向其中添加了一些方法,比如<code>Object.prototype</code>,它非常有可能按照某种不确定的方式破坏代码。你几乎永远都需要使用先前方法中的一个去用来表述JSON。</p>
<h2 id="See_also" name="See_also">还可以看</h2>
<ul>
  <li>{{ interface("nsIJSON") }}</li>
</ul>
<p>{{ languages( { "ja": "ja/JSON", "zh-cn": "zh-cn/JSON" } ) }}</p>
恢复到这个版本