mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-12-15 15:20:30 +08:00
语法高亮,滚动条美化,设置页面调整
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
<div></div>
|
||||
<p><code><strong>JSON.stringify()</strong></code> 方法是将一个JavaScript值(对象或者数组)转换为一个 JSON字符串,如果指定了replacer是一个函数,则可以替换值,或者如果指定了replacer是一个数组,可选的仅包括指定的属性。</p>
|
||||
<h2 id="Syntax" name="Syntax">语法</h2>
|
||||
<pre class="syntaxbox"><code>JSON.stringify(<em>value</em>[, <em>replacer</em> [, <em>space</em>]])</code>
|
||||
</pre>
|
||||
<pre><code class="language-javascript"><code>JSON.stringify(<em>value</em>[, <em>replacer</em> [, <em>space</em>]])</code>
|
||||
</code></pre>
|
||||
<h3 id="Parameters" name="Parameters">参数</h3>
|
||||
<dl>
|
||||
<dt><code>value</code></dt>
|
||||
@@ -28,7 +28,7 @@
|
||||
<li>所有以 symbol 为属性键的属性都会被完全忽略掉,即便 <code>replacer</code> 参数中强制指定包含了它们。</li>
|
||||
<li>不可枚举的属性会被忽略</li>
|
||||
</ul>
|
||||
<pre class="brush: js">JSON.stringify({}); // '{}'
|
||||
<pre><code class="language-javascript">JSON.stringify({}); // '{}'
|
||||
JSON.stringify(true); // 'true'
|
||||
JSON.stringify("foo"); // '"foo"'
|
||||
JSON.stringify([1, "false", false]); // '[1,"false",false]'
|
||||
@@ -76,7 +76,7 @@ JSON.stringify(
|
||||
);
|
||||
|
||||
// "{"y":"y"}"
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="replacer参数"><code>replacer参数</code></h3>
|
||||
<p>replacer参数可以是一个函数或者一个数组。作为函数,它有两个参数,键(key)值(value)都会被序列化。</p>
|
||||
<ul>
|
||||
@@ -96,25 +96,25 @@ JSON.stringify(
|
||||
}
|
||||
|
||||
var foo = {foundation: "Mozilla", model: "box", week: 45, transport: "car", month: 7};
|
||||
var jsonString = JSON.stringify(foo, replacer);</code></pre>
|
||||
var jsonString = JSON.stringify(foo, replacer);</code></code></pre>
|
||||
<p>JSON序列化结果为 <code>{"week":45,"month":7}</code>.</p>
|
||||
<h4 id="例子(array)">例子(array)</h4>
|
||||
<p><code>如果replacer是一个数组,数组的值代表将被序列化成JSON字符串的属性名。</code></p>
|
||||
<pre><code>JSON.stringify(foo, ['week', 'month']);
|
||||
// '{"week":45,"month":7}', 只保留“week”和“month”属性值。</code></pre>
|
||||
// '{"week":45,"month":7}', 只保留“week”和“month”属性值。</code></code></pre>
|
||||
<h3 id="space_参数"><code>space</code> 参数</h3>
|
||||
<p>space 参数用来控制结果字符串里面的间距。如果是一个数字, 则在字符串化时每一级别会比上一级别缩进多这个数字值的空格(最多10个空格);如果是一个字符串,则每一级别会比上一级别多缩进用该字符串(或该字符串的前十个字符)。</p>
|
||||
<pre class="brush: js">JSON.stringify({ a: 2 }, null, " "); // '{\n "a": 2\n}'</pre>
|
||||
<pre><code class="language-javascript">JSON.stringify({ a: 2 }, null, " "); // '{\n "a": 2\n}'</code></pre>
|
||||
<p>使用制表符(\t)来缩进:</p>
|
||||
<pre class="brush: js">JSON.stringify({ uno: 1, dos : 2 }, null, '\t')
|
||||
<pre><code class="language-javascript">JSON.stringify({ uno: 1, dos : 2 }, null, '\t')
|
||||
// '{ \
|
||||
// "uno": 1, \
|
||||
// "dos": 2 \
|
||||
// }'
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="toJSON_方法">toJSON 方法</h3>
|
||||
<p>如果一个被序列化的对象拥有 <code>toJSON</code> 方法,那么该 <code>toJSON</code> 方法就会覆盖该对象默认的序列化行为:不是那个对象被序列化,而是调用 <code>toJSON</code> 方法后的返回值会被序列化,例如:</p>
|
||||
<pre class="brush: js">var obj = {
|
||||
<pre><code class="language-javascript">var obj = {
|
||||
foo: 'foo',
|
||||
toJSON: function () {
|
||||
return 'bar';
|
||||
@@ -122,7 +122,7 @@ var jsonString = JSON.stringify(foo, replacer);</code></pre>
|
||||
};
|
||||
JSON.stringify(obj); // <code>'"bar"'</code>
|
||||
JSON.stringify({x: obj}); // <code>'{"x":"bar"}'</code>
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h3 id="JSON.stringify用作_JavaScript"><code>JSON.stringify</code>用作 JavaScript</h3>
|
||||
<p>注意JSON不是javascript严格意义上的子集,在JSON中不需要省略两条终线(Line separator和Paragraph separator)但在JavaScript中需要被省略。因此,如果JSON被用作JSONP时,下面方法可以使用:</p>
|
||||
<pre><code>function jsFriendlyJSONStringify (s) {
|
||||
@@ -146,10 +146,10 @@ eval('(' + jsFriendlyJSONStringify(s) + ')');
|
||||
|
||||
// console.log in Firefox unescapes the Unicode if
|
||||
// logged to console, so we use alert
|
||||
alert(jsFriendlyJSONStringify(s)); // {"a":"\u2028","b":"\u2029"}</code></pre>
|
||||
alert(jsFriendlyJSONStringify(s)); // {"a":"\u2028","b":"\u2029"}</code></code></pre>
|
||||
<h3 id="使用_JSON.stringify_结合_localStorage_的例子">使用 JSON.stringify 结合 localStorage 的例子</h3>
|
||||
<p>一些时候,你想存储用户创建的一个对象,并且,即使在浏览器被关闭后仍能恢复该对象。下面的例子是 <code>JSON.stringify</code> 适用于这种情形的一个样板:</p>
|
||||
<pre class="brush: js">// 创建一个示例数据
|
||||
<pre><code class="language-javascript">// 创建一个示例数据
|
||||
var session = {
|
||||
'screens' : [],
|
||||
'state' : true
|
||||
@@ -170,7 +170,7 @@ var restoredSession = JSON.parse(localStorage.getItem('session'));
|
||||
|
||||
// 现在 restoredSession 包含了保存在 localStorage 里的对象
|
||||
console.log(restoredSession);
|
||||
</pre>
|
||||
</code></pre>
|
||||
<h2 id="规范">规范</h2>
|
||||
<table class="standard-table">
|
||||
<tbody>
|
||||
|
||||
Reference in New Issue
Block a user