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

184 lines
12 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>返回最后发生的错误</title>
</head>
<body class="docs"><div id="layout">
<div id="layout-content"><div id="function.json-last-error" class="refentry">
<div class="refnamediv">
<h1 class="refname">json_last_error</h1>
<p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7)</p><p class="refpurpose"><span class="refname">json_last_error</span> &mdash; <span class="dc-title">返回最后发生的错误</span></p>
</div>
<div class="refsect1 description" id="refsect1-function.json-last-error-description">
<h3 class="title">说明</h3>
<div class="methodsynopsis dc-description">
<span class="methodname"><strong>json_last_error</strong></span>
( <span class="methodparam">void</span>
) : <span class="type">int</span></div>
<p class="para rdfs-comment">
如果有,返回 JSON 编码解码时最后发生的错误。
</p>
</div>
<div class="refsect1 parameters" id="refsect1-function.json-last-error-parameters">
<h3 class="title">参数</h3>
<p class="para">此函数没有参数。</p>
</div>
<div class="refsect1 returnvalues" id="refsect1-function.json-last-error-returnvalues">
<h3 class="title">返回值</h3>
<p class="para">
返回一个整型integer这个值会是以下的常量之一
</p>
<table class="doctable table">
<caption><strong>JSON 错误码</strong></caption>
<thead>
<tr>
<th>常量</th>
<th>含义</th>
<th>可用性</th>
</tr>
</thead>
<tbody class="tbody">
<tr>
<td><strong><code>JSON_ERROR_NONE</code></strong></td>
<td>没有错误发生</td>
<td class="empty">&nbsp;</td>
</tr>
<tr>
<td><strong><code>JSON_ERROR_DEPTH</code></strong></td>
<td>到达了最大堆栈深度</td>
<td class="empty">&nbsp;</td>
</tr>
<tr>
<td><strong><code>JSON_ERROR_STATE_MISMATCH</code></strong></td>
<td>无效或异常的 JSON</td>
<td class="empty">&nbsp;</td>
</tr>
<tr>
<td><strong><code>JSON_ERROR_CTRL_CHAR</code></strong></td>
<td>控制字符错误,可能是编码不对</td>
<td class="empty">&nbsp;</td>
</tr>
<tr>
<td><strong><code>JSON_ERROR_SYNTAX</code></strong></td>
<td>语法错误</td>
<td class="empty">&nbsp;</td>
</tr>
<tr>
<td><strong><code>JSON_ERROR_UTF8</code></strong></td>
<td>异常的 UTF-8 字符,也许是因为不正确的编码。</td>
<td>PHP 5.3.3</td>
</tr>
<tr>
<td><strong><code>JSON_ERROR_RECURSION</code></strong></td>
<td>One or more recursive references in the value to be encoded</td>
<td>PHP 5.5.0</td>
</tr>
<tr>
<td><strong><code>JSON_ERROR_INF_OR_NAN</code></strong></td>
<td>
One or more
<a href="language.types.float.html#language.types.float.nan" class="link"><strong><code>NAN</code></strong></a>
or <a href="is_infinite.html" class="link"><strong><code>INF</code></strong></a>
values in the value to be encoded
</td>
<td>PHP 5.5.0</td>
</tr>
<tr>
<td><strong><code>JSON_ERROR_UNSUPPORTED_TYPE</code></strong></td>
<td>指定的类型,值无法编码。</td>
<td>PHP 5.5.0</td>
</tr>
<tr>
<td><strong><code>JSON_ERROR_INVALID_PROPERTY_NAME</code></strong></td>
<td>指定的属性名无法编码。</td>
<td>PHP 7.0.0</td>
</tr>
<tr>
<td><strong><code>JSON_ERROR_UTF16</code></strong></td>
<td>畸形的 UTF-16 字符,可能因为字符编码不正确。</td>
<td>PHP 7.0.0</td>
</tr>
</tbody>
</table>
</div>
<div class="refsect1 examples" id="refsect1-function.json-last-error-examples">
<h3 class="title">范例</h3>
<p class="para">
<div class="example" id="example-4523">
<p><strong>Example #1 <span class="function"><strong>json_last_error()</strong></span> 例子</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;一个有效的&nbsp;json&nbsp;字符串<br /></span><span style="color: #0000BB">$json</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #DD0000">'{"Organization":&nbsp;"PHP&nbsp;Documentation&nbsp;Team"}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;一个无效的&nbsp;json&nbsp;字符串会导致一个语法错误,在这个例子里我们使用&nbsp;'&nbsp;代替了&nbsp;"&nbsp;作为引号<br /></span><span style="color: #0000BB">$json</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #DD0000">"{'Organization':&nbsp;'PHP&nbsp;Documentation&nbsp;Team'}"</span><span style="color: #007700">;<br /><br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$json&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'Decoding:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">json_decode</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;switch&nbsp;(</span><span style="color: #0000BB">json_last_error</span><span style="color: #007700">())&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">JSON_ERROR_NONE</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;No&nbsp;errors'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">JSON_ERROR_DEPTH</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;Maximum&nbsp;stack&nbsp;depth&nbsp;exceeded'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">JSON_ERROR_STATE_MISMATCH</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;Underflow&nbsp;or&nbsp;the&nbsp;modes&nbsp;mismatch'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">JSON_ERROR_CTRL_CHAR</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;Unexpected&nbsp;control&nbsp;character&nbsp;found'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">JSON_ERROR_SYNTAX</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;Syntax&nbsp;error,&nbsp;malformed&nbsp;JSON'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">JSON_ERROR_UTF8</span><span style="color: #007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;Malformed&nbsp;UTF-8&nbsp;characters,&nbsp;possibly&nbsp;incorrectly&nbsp;encoded'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">'&nbsp;-&nbsp;Unknown&nbsp;error'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;echo&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>
Decoding: {&quot;Organization&quot;: &quot;PHP Documentation Team&quot;} - No errors
Decoding: {&#039;Organization&#039;: &#039;PHP Documentation Team&#039;} - Syntax error, malformed JSON
</pre></div>
</div>
</div>
</p>
<p class="para">
<div class="example" id="example-4524">
<p><strong>Example #2 <span class="function"><a href="json_encode.html" class="function">json_encode()</a></span><span class="function"><strong>json_last_error()</strong></span></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;无效的&nbsp;UTF8&nbsp;序列<br /></span><span style="color: #0000BB">$text&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"\xB1\x31"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$json&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">json_encode</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$error&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">json_last_error</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</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$error&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">JSON_ERROR_UTF8</span><span style="color: #007700">);<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>
string(4) &quot;null&quot;
bool(true)
</pre></div>
</div>
</div>
</p>
</div>
<div class="refsect1 seealso" id="refsect1-function.json-last-error-seealso">
<h3 class="title">参见</h3>
<p class="para">
<ul class="simplelist">
<li class="member"><span class="function"><a href="json_last_error_msg.html" class="function" rel="rdfs-seeAlso">json_last_error_msg()</a> - Returns the error string of the last json_encode() or json_decode() call</span></li>
<li class="member"><span class="function"><a href="json_decode.html" class="function" rel="rdfs-seeAlso">json_decode()</a> - 对 JSON 格式的字符串进行解码</span></li>
<li class="member"><span class="function"><a href="json_encode.html" class="function" rel="rdfs-seeAlso">json_encode()</a> - 对变量进行 JSON 编码</span></li>
</ul>
</p>
</div>
</div></div></div></body></html>