uTools-Manuals/docs/php/json_decode.html
2019-04-08 23:22:26 +08:00

346 lines
20 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>对 JSON 格式的字符串进行解码</title>
</head>
<body class="docs"><div id="layout">
<div id="layout-content"><div id="function.json-decode" class="refentry">
<div class="refnamediv">
<h1 class="refname">json_decode</h1>
<p class="verinfo">(PHP 5 &gt;= 5.2.0, PHP 7, PECL json &gt;= 1.2.0)</p><p class="refpurpose"><span class="refname">json_decode</span> &mdash; <span class="dc-title">对 JSON 格式的字符串进行解码</span></p>
</div>
<div class="refsect1 description" id="refsect1-function.json-decode-description">
<h3 class="title">说明</h3>
<div class="methodsynopsis dc-description">
<span class="methodname"><strong>json_decode</strong></span>
( <span class="methodparam"><span class="type">string</span> <code class="parameter">$json</code></span>
[, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$assoc</code><span class="initializer"> = false</span></span>
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$depth</code><span class="initializer"> = 512</span></span>
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$options</code><span class="initializer"> = 0</span></span>
]]] ) : <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span></div>
<p class="para rdfs-comment">
接受一个 JSON 编码的字符串并且把它转换为 PHP 变量
</p>
</div>
<div class="refsect1 parameters" id="refsect1-function.json-decode-parameters">
<h3 class="title">参数</h3>
<p class="para">
<dl>
<dt>
<code class="parameter">json</code></dt>
<dd>
<p class="para">
待解码的 <code class="parameter">json</code> <span class="type"><a href="language.types.string.html" class="type string">string</a></span> 格式的字符串。
</p>
<p class="para">
这个函数仅能处理 UTF-8 编码的数据。
</p>
<blockquote class="note"><p><strong class="note">Note</strong>:
<p class="para">
PHP implements a superset of JSON as specified in the original
<a href="http://www.faqs.org/rfcs/rfc7159" class="link external">&raquo;&nbsp;RFC 7159</a>.
</p>
</p></blockquote>
</dd>
<dt>
<code class="parameter">assoc</code></dt>
<dd>
<p class="para">
当该参数为 <strong><code>TRUE</code></strong> 时,将返回 <span class="type"><a href="language.types.array.html" class="type array">array</a></span> 而非 <span class="type"><a href="language.types.object.html" class="type object">object</a></span>
</p>
</dd>
<dt>
<code class="parameter">depth</code></dt>
<dd>
<p class="para">
指定递归深度。
</p>
</dd>
<dt>
<code class="parameter">options</code></dt>
<dd>
<p class="para">
JSON解码的掩码选项。 现在有两个支持的选项。
第一个是<strong><code>JSON_BIGINT_AS_STRING</code></strong>
用于将大整数转为字符串而非默认的float类型。第二个是
<strong><code>JSON_OBJECT_AS_ARRAY</code></strong>
与将<code class="parameter">assoc</code>设置为 <strong><code>TRUE</code></strong> 有相同的效果。
</p>
</dd>
</dl>
</p>
</div>
<div class="refsect1 returnvalues" id="refsect1-function.json-decode-returnvalues">
<h3 class="title">返回值</h3>
<p class="para">
Returns the value encoded in <code class="parameter">json</code> in appropriate
PHP type. Values <em>true</em>, <em>false</em> and
<em>null</em> are returned as <strong><code>TRUE</code></strong>, <strong><code>FALSE</code></strong> and <strong><code>NULL</code></strong>
respectively. <strong><code>NULL</code></strong> is returned if the <code class="parameter">json</code> cannot
be decoded or if the encoded data is deeper than the recursion limit.
</p>
</div>
<div class="refsect1 examples" id="refsect1-function.json-decode-examples">
<h3 class="title">范例</h3>
<p class="para">
<div class="example" id="example-4513">
<p><strong>Example #1 <span class="function"><strong>json_decode()</strong></span> 的例子</strong></p>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'{"a":1,"b":2,"c":3,"d":4,"e":5}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
<div class="example-contents"><p>以上例程会输出:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
object(stdClass)#1 (5) {
[&quot;a&quot;] =&gt; int(1)
[&quot;b&quot;] =&gt; int(2)
[&quot;c&quot;] =&gt; int(3)
[&quot;d&quot;] =&gt; int(4)
[&quot;e&quot;] =&gt; int(5)
}
array(5) {
[&quot;a&quot;] =&gt; int(1)
[&quot;b&quot;] =&gt; int(2)
[&quot;c&quot;] =&gt; int(3)
[&quot;d&quot;] =&gt; int(4)
[&quot;e&quot;] =&gt; int(5)
}
</pre></div>
</div>
</div>
<div class="example" id="example-4514">
<p><strong>Example #2 Accessing invalid object properties</strong></p>
<div class="example-contents"><p>
Accessing elements within an object that contain characters not
permitted under PHP&#039;s naming convention (e.g. the hyphen) can be
accomplished by encapsulating the element name within braces and the apostrophe.
</p></div>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'{"foo-bar":&nbsp;12345}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$obj&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">);<br />print&nbsp;</span><span style="color: #0000BB">$obj</span><span style="color: #007700">-&gt;{</span><span style="color: #DD0000">'foo-bar'</span><span style="color: #007700">};&nbsp;</span><span style="color: #FF8000">//&nbsp;12345<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
</div>
<div class="example" id="example-4515">
<p><strong>Example #3 common mistakes using <span class="function"><strong>json_decode()</strong></span></strong></p>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">//&nbsp;the&nbsp;following&nbsp;strings&nbsp;are&nbsp;valid&nbsp;JavaScript&nbsp;but&nbsp;not&nbsp;valid&nbsp;JSON<br /><br />//&nbsp;the&nbsp;name&nbsp;and&nbsp;value&nbsp;must&nbsp;be&nbsp;enclosed&nbsp;in&nbsp;double&nbsp;quotes<br />//&nbsp;single&nbsp;quotes&nbsp;are&nbsp;not&nbsp;valid&nbsp;<br /></span><span style="color: #0000BB">$bad_json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"{&nbsp;'bar':&nbsp;'baz'&nbsp;}"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$bad_json</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;null<br /><br />//&nbsp;the&nbsp;name&nbsp;must&nbsp;be&nbsp;enclosed&nbsp;in&nbsp;double&nbsp;quotes<br /></span><span style="color: #0000BB">$bad_json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'{&nbsp;bar:&nbsp;"baz"&nbsp;}'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$bad_json</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;null<br /><br />//&nbsp;trailing&nbsp;commas&nbsp;are&nbsp;not&nbsp;allowed<br /></span><span style="color: #0000BB">$bad_json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'{&nbsp;bar:&nbsp;"baz",&nbsp;}'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$bad_json</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;null<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
</div>
<div class="example" id="example-4516">
<p><strong>Example #4 <code class="parameter">depth</code> errors</strong></p>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Encode&nbsp;the&nbsp;data.<br /></span><span style="color: #0000BB">$json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">1&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'English'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'One'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'January'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'French'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'Une'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'Janvier'<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)<br />&nbsp;&nbsp;&nbsp;&nbsp;)<br />);<br /><br /></span><span style="color: #FF8000">//&nbsp;Define&nbsp;the&nbsp;errors.<br /></span><span style="color: #0000BB">$constants&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">get_defined_constants</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$json_errors&nbsp;</span><span style="color: #007700">=&nbsp;array();<br />foreach&nbsp;(</span><span style="color: #0000BB">$constants</span><span style="color: #007700">[</span><span style="color: #DD0000">"json"</span><span style="color: #007700">]&nbsp;as&nbsp;</span><span style="color: #0000BB">$name&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$value</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!</span><span style="color: #0000BB">strncmp</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"JSON_ERROR_"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">11</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$json_errors</span><span style="color: #007700">[</span><span style="color: #0000BB">$value</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Show&nbsp;the&nbsp;errors&nbsp;for&nbsp;different&nbsp;depths.<br /></span><span style="color: #007700">foreach&nbsp;(</span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;as&nbsp;</span><span style="color: #0000BB">$depth</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$depth</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Last&nbsp;error:&nbsp;'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$json_errors</span><span style="color: #007700">[</span><span style="color: #0000BB">json_last_error</span><span style="color: #007700">()],&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
<div class="example-contents"><p>以上例程会输出:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
array(1) {
[1]=&gt;
array(2) {
[&quot;English&quot;]=&gt;
array(2) {
[0]=&gt;
string(3) &quot;One&quot;
[1]=&gt;
string(7) &quot;January&quot;
}
[&quot;French&quot;]=&gt;
array(2) {
[0]=&gt;
string(3) &quot;Une&quot;
[1]=&gt;
string(7) &quot;Janvier&quot;
}
}
}
Last error: JSON_ERROR_NONE
NULL
Last error: JSON_ERROR_DEPTH
</pre></div>
</div>
</div>
<div class="example" id="example-4517">
<p><strong>Example #5 <span class="function"><strong>json_decode()</strong></span> of large integers</strong></p>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$json&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'{"number":&nbsp;12345678901234567890}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$json</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">512</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">JSON_BIGINT_AS_STRING</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
<div class="example-contents"><p>以上例程会输出:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
object(stdClass)#1 (1) {
[&quot;number&quot;]=&gt;
float(1.2345678901235E+19)
}
object(stdClass)#1 (1) {
[&quot;number&quot;]=&gt;
string(20) &quot;12345678901234567890&quot;
}
</pre></div>
</div>
</div>
</p>
</div>
<div class="refsect1 notes" id="refsect1-function.json-decode-notes">
<h3 class="title">注释</h3>
<blockquote class="note"><p><strong class="note">Note</strong>:
<p class="para">
The JSON spec is not JavaScript, but a subset of JavaScript.
</p>
</p></blockquote>
<blockquote class="note"><p><strong class="note">Note</strong>:
<p class="para">
In the event of a failure to decode, <span class="function"><a href="json_last_error.html" class="function">json_last_error()</a></span>
can be used to determine the exact nature of the error.
</p>
</p></blockquote>
</div>
<div class="refsect1 changelog" id="refsect1-function.json-decode-changelog">
<h3 class="title">更新日志</h3>
<p class="para">
<table class="doctable informaltable">
<thead>
<tr>
<th>版本</th>
<th>说明</th>
</tr>
</thead>
<tbody class="tbody">
<tr>
<td>7.1.0</td>
<td>
An empty JSON key (&quot;&quot;) can be encoded to the empty object property
instead of using a key with value <em>_empty_</em>.
</td>
</tr>
<tr>
<td>7.0.0</td>
<td>
Rejected RFC 7159 incompatible number formats - top level
(07, 0xff, .1, -.1) and all levels ([1.], [1.e1])
</td>
</tr>
<tr>
<td>7.0.0</td>
<td>
An empty PHP string or value that after casting to string is an empty
string (<em>NULL</em>, <em>FALSE</em>) results
in JSON syntax error.
</td>
</tr>
<tr>
<td>5.6.0</td>
<td>
Invalid non-lowercased variants of the <em>true</em>,
<em>false</em> and <em>null</em> literals are no
longer accepted as valid input, and will generate warnings.
</td>
</tr>
<tr>
<td>5.4.0</td>
<td>
The <code class="parameter">options</code> parameter was added.
</td>
</tr>
<tr>
<td>5.3.0</td>
<td>Added the optional <code class="parameter">depth</code>. The default recursion depth was increased from 128 to 512</td>
</tr>
<tr>
<td>5.2.3</td>
<td>The nesting limit was increased from 20 to 128</td>
</tr>
<tr>
<td>5.2.1</td>
<td>
Added support for JSON decoding of basic types.
</td>
</tr>
</tbody>
</table>
</p>
</div>
<div class="refsect1 seealso" id="refsect1-function.json-decode-seealso">
<h3 class="title">参见</h3>
<p class="para">
<ul class="simplelist">
<li class="member"><span class="function"><a href="json_encode.html" class="function" rel="rdfs-seeAlso">json_encode()</a> - 对变量进行 JSON 编码</span></li>
<li class="member"><span class="function"><a href="json_last_error.html" class="function" rel="rdfs-seeAlso">json_last_error()</a> - 返回最后发生的错误</span></li>
</ul>
</p>
</div>
</div></div></div></body></html>