JSON

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

修订内容

JSON (JavaScript Object Notation)是一种数据交换格式,是以JavaScrip语法的一个子集为基础的。可以用于书写任何类型的 JavaScrip 为基础的应用,包括扩展。参阅 http://json.org/ 获取细节。

可以预期,未来的某个版本的 JavaScript({{ Bug(340987) }})将会内置 JSON 支持。

One great use of JSON is to easily store objects in preferences as char preferences. {{ 注意("本文的剩余部分是针对扩展和 XUL 应用开发人员进行了剪裁的。") }}

Firefox 2里面的 JSON

No JSON support is included in Firefox 2. Using the code from json.org can be problematic and raise variations on {{ Bug(397595) }}. You should adapt the code from json.org or from {{ Source("js/src/xpconnect/loader/JSON.jsm", "JSON.jsm") }}, which provides a JSON object which can be used to serialize to JSON and parse JSON.

JSON in Firefox 3

Firefox 3 introduced the {{ Interface("nsIJSON") }} interface, which provides easy to use support for encoding and decoding JSON strings from JavaScript code. Firefox 3 also includes a JavaScript code module for JSON encoding and decoding, JSON.jsm. The code module can be imported using Components.utils.import.

Using JSON

Quick Warning

When writing privileged code, such as extensions, be careful to not just eval JSON strings (any strings, actually) from untrusted source. Either use a JSON decoder (best), Components.utils.evalInSandbox, or parseJSON from json.org. See Downloading JSON and JavaScript in extensions.

Using JSON.jsm

To serialize a javascript object:

Components.utils.import("resource://gre/modules/json.jsm");

var foo = {};
foo.bar = "new property";
foo.baz = 3;
var JSONfoo = JSON.toString(foo);

The JSONfoo now holds {"bar":"new property","baz":3}. To make JSONfoo back into a JavaScript object just do:

var backToJS = JSON.fromString(JSONfoo);

Using nsIJSON

To serialize a javascript object:

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);

The JSONfoo now holds {"bar":"new property","baz":3}. To make JSONfoo back into a JavaScript object just do:

var backToJS = nativeJSON.decode(JSONfoo);

Limitations

You cannot serialize an object that has members which are functions. The JSON specification does not allow it. For example:

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

will raise a TypeError on line XXX: No JSON representation for this object!

See also

{{ languages( { "ja": "ja/JSON", "en": "en/JSON" } ) }}

修订版来源

<p><b>JSON</b> (<b>JavaScript Object Notation</b>)是一种数据交换格式,是以<a href="cn/JavaScrip">JavaScrip</a>语法的一个子集为基础的。可以用于书写任何类型的 JavaScrip 为基础的应用,包括扩展。参阅 <a class=" external" href="http://json.org/" rel="freelink">http://json.org/</a> 获取细节。
</p><p>可以预期,未来的某个版本的 JavaScript({{ Bug(340987) }})将会内置 JSON 支持。
</p><p><span class="comment">One great use of JSON is to easily store objects in preferences as char preferences.</span>
{{ 注意("本文的剩余部分是针对扩展和 XUL 应用开发人员进行了剪裁的。") }}
</p>
<h2 id="Firefox_2.E9.87.8C.E9.9D.A2.E7.9A.84_JSON" name="Firefox_2.E9.87.8C.E9.9D.A2.E7.9A.84_JSON">Firefox 2里面的 JSON</h2>
<p>No JSON support is included in Firefox 2. Using the code from <a class="external" href="http://json.org">json.org</a> can be problematic and raise variations on {{ Bug(397595) }}. You should adapt the code from json.org or from {{ Source("js/src/xpconnect/loader/JSON.jsm", "JSON.jsm") }}, which provides a JSON object which can be used to serialize to JSON and parse JSON.
</p>
<h2 id="JSON_in_Firefox_3" name="JSON_in_Firefox_3"> JSON in Firefox 3 </h2>
<p>Firefox 3 introduced the {{ Interface("nsIJSON") }} interface, which provides easy to use support for encoding and decoding JSON strings from JavaScript code. Firefox 3 also includes a <a href="cn/Using_JavaScript_code_modules">JavaScript code module</a> for JSON encoding and decoding, JSON.jsm. The code module can be imported using <code><a href="cn/Components.utils.import">Components.utils.import</a></code>.
</p>
<h2 id="Using_JSON" name="Using_JSON"> Using JSON </h2>
<h3 id="Quick_Warning" name="Quick_Warning"> Quick Warning </h3>
<p>When writing privileged code, such as extensions, be careful to not just <a href="cn/Core_JavaScript_1.5_Reference/Global_Functions/eval">eval</a> JSON strings (any strings, actually) from untrusted source. Either use a JSON decoder (best), <a href="cn/Components.utils.evalInSandbox">Components.utils.evalInSandbox</a>, or <code>parseJSON</code> from json.org. See <a href="cn/Downloading_JSON_and_JavaScript_in_extensions">Downloading JSON and JavaScript in extensions</a>.
</p>
<h3 id="Using_JSON.jsm" name="Using_JSON.jsm"> Using JSON.jsm </h3>
<p>To serialize a javascript object: </p>
<pre class="eval">Components.utils.import("<a class=" external" href="resource://gre/modules/json.jsm" rel="freelink">resource://gre/modules/json.jsm</a>");

var foo = {};
foo.bar = "new property";
foo.baz = 3;
var JSONfoo = JSON.toString(foo);
</pre>
<p>The <code>JSONfoo</code> now holds <code>{"bar":"new property","baz":3}</code>. To make <code>JSONfoo</code> back into a JavaScript object just do:
</p>
<pre class="eval">var backToJS = JSON.fromString(JSONfoo);
</pre>
<h3 id="Using_nsIJSON" name="Using_nsIJSON"> Using nsIJSON </h3>
<p>To serialize a javascript object: </p>
<pre class="eval">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>The <code>JSONfoo</code> now holds <code>{"bar":"new property","baz":3}</code>. To make <code>JSONfoo</code> back into a JavaScript object just do:
</p>
<pre class="eval">var backToJS = nativeJSON.decode(JSONfoo);
</pre>
<h3 id="Limitations" name="Limitations"> Limitations </h3>
<p>You cannot serialize an object that has members which are functions. The JSON specification does not allow it. For example:
</p>
<pre class="eval">foo.qwerty = function(){alert('foobar');};
foo.qwerty()
var JSONfoo = JSON.toString(foo);
</pre>
<p>will raise a <code>TypeError on line XXX: No JSON representation for this object!</code>
</p>
<h3 id="See_also" name="See_also">See also</h3>
<ul><li> <a href="cn/JSON/JSONPath">JSONPath</a>
</li></ul>
<div class="noinclude">
</div>
{{ languages( { "ja": "ja/JSON", "en": "en/JSON" } ) }}
恢复到这个版本