mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-18 13:57:03 +08:00
346 lines
20 KiB
HTML
346 lines
20 KiB
HTML
<!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 >= 5.2.0, PHP 7, PECL json >= 1.2.0)</p><p class="refpurpose"><span class="refname">json_decode</span> — <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">» 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"><?php<br />$json </span><span style="color: #007700">= </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">, </span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?></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) {
|
||
["a"] => int(1)
|
||
["b"] => int(2)
|
||
["c"] => int(3)
|
||
["d"] => int(4)
|
||
["e"] => int(5)
|
||
}
|
||
|
||
array(5) {
|
||
["a"] => int(1)
|
||
["b"] => int(2)
|
||
["c"] => int(3)
|
||
["d"] => int(4)
|
||
["e"] => 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'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"><?php<br /><br />$json </span><span style="color: #007700">= </span><span style="color: #DD0000">'{"foo-bar": 12345}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$obj </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 />print </span><span style="color: #0000BB">$obj</span><span style="color: #007700">->{</span><span style="color: #DD0000">'foo-bar'</span><span style="color: #007700">}; </span><span style="color: #FF8000">// 12345<br /><br /></span><span style="color: #0000BB">?></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"><?php<br /><br /></span><span style="color: #FF8000">// the following strings are valid JavaScript but not valid JSON<br /><br />// the name and value must be enclosed in double quotes<br />// single quotes are not valid <br /></span><span style="color: #0000BB">$bad_json </span><span style="color: #007700">= </span><span style="color: #DD0000">"{ 'bar': 'baz' }"</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">); </span><span style="color: #FF8000">// null<br /><br />// the name must be enclosed in double quotes<br /></span><span style="color: #0000BB">$bad_json </span><span style="color: #007700">= </span><span style="color: #DD0000">'{ bar: "baz" }'</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">); </span><span style="color: #FF8000">// null<br /><br />// trailing commas are not allowed<br /></span><span style="color: #0000BB">$bad_json </span><span style="color: #007700">= </span><span style="color: #DD0000">'{ bar: "baz", }'</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">); </span><span style="color: #FF8000">// null<br /><br /></span><span style="color: #0000BB">?></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"><?php<br /></span><span style="color: #FF8000">// Encode the data.<br /></span><span style="color: #0000BB">$json </span><span style="color: #007700">= </span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(<br /> array(<br /> </span><span style="color: #0000BB">1 </span><span style="color: #007700">=> array(<br /> </span><span style="color: #DD0000">'English' </span><span style="color: #007700">=> array(<br /> </span><span style="color: #DD0000">'One'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'January'<br /> </span><span style="color: #007700">),<br /> </span><span style="color: #DD0000">'French' </span><span style="color: #007700">=> array(<br /> </span><span style="color: #DD0000">'Une'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'Janvier'<br /> </span><span style="color: #007700">)<br /> )<br /> )<br />);<br /><br /></span><span style="color: #FF8000">// Define the errors.<br /></span><span style="color: #0000BB">$constants </span><span style="color: #007700">= </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 </span><span style="color: #007700">= array();<br />foreach (</span><span style="color: #0000BB">$constants</span><span style="color: #007700">[</span><span style="color: #DD0000">"json"</span><span style="color: #007700">] as </span><span style="color: #0000BB">$name </span><span style="color: #007700">=> </span><span style="color: #0000BB">$value</span><span style="color: #007700">) {<br /> if (!</span><span style="color: #0000BB">strncmp</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">, </span><span style="color: #DD0000">"JSON_ERROR_"</span><span style="color: #007700">, </span><span style="color: #0000BB">11</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">$json_errors</span><span style="color: #007700">[</span><span style="color: #0000BB">$value</span><span style="color: #007700">] = </span><span style="color: #0000BB">$name</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #FF8000">// Show the errors for different depths.<br /></span><span style="color: #007700">foreach (</span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">) as </span><span style="color: #0000BB">$depth</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">, </span><span style="color: #0000BB">true</span><span style="color: #007700">, </span><span style="color: #0000BB">$depth</span><span style="color: #007700">));<br /> echo </span><span style="color: #DD0000">'Last error: '</span><span style="color: #007700">, </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">()], </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span>
|
||
</span>
|
||
</pre></div>
|
||
</div>
|
||
|
||
<div class="example-contents"><p>以上例程会输出:</p></div>
|
||
<div class="example-contents screen">
|
||
<div class="cdata"><pre>
|
||
array(1) {
|
||
[1]=>
|
||
array(2) {
|
||
["English"]=>
|
||
array(2) {
|
||
[0]=>
|
||
string(3) "One"
|
||
[1]=>
|
||
string(7) "January"
|
||
}
|
||
["French"]=>
|
||
array(2) {
|
||
[0]=>
|
||
string(3) "Une"
|
||
[1]=>
|
||
string(7) "Janvier"
|
||
}
|
||
}
|
||
}
|
||
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"><?php<br />$json </span><span style="color: #007700">= </span><span style="color: #DD0000">'{"number": 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">, </span><span style="color: #0000BB">false</span><span style="color: #007700">, </span><span style="color: #0000BB">512</span><span style="color: #007700">, </span><span style="color: #0000BB">JSON_BIGINT_AS_STRING</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?></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) {
|
||
["number"]=>
|
||
float(1.2345678901235E+19)
|
||
}
|
||
object(stdClass)#1 (1) {
|
||
["number"]=>
|
||
string(20) "12345678901234567890"
|
||
}
|
||
</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 ("") 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> |