mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-18 22:06:57 +08:00
184 lines
12 KiB
HTML
184 lines
12 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>返回最后发生的错误</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 >= 5.3.0, PHP 7)</p><p class="refpurpose"><span class="refname">json_last_error</span> — <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"> </td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td><strong><code>JSON_ERROR_DEPTH</code></strong></td>
|
||
<td>到达了最大堆栈深度</td>
|
||
<td class="empty"> </td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td><strong><code>JSON_ERROR_STATE_MISMATCH</code></strong></td>
|
||
<td>无效或异常的 JSON</td>
|
||
<td class="empty"> </td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td><strong><code>JSON_ERROR_CTRL_CHAR</code></strong></td>
|
||
<td>控制字符错误,可能是编码不对</td>
|
||
<td class="empty"> </td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td><strong><code>JSON_ERROR_SYNTAX</code></strong></td>
|
||
<td>语法错误</td>
|
||
<td class="empty"> </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"><?php<br /></span><span style="color: #FF8000">// 一个有效的 json 字符串<br /></span><span style="color: #0000BB">$json</span><span style="color: #007700">[] = </span><span style="color: #DD0000">'{"Organization": "PHP Documentation Team"}'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// 一个无效的 json 字符串会导致一个语法错误,在这个例子里我们使用 ' 代替了 " 作为引号<br /></span><span style="color: #0000BB">$json</span><span style="color: #007700">[] = </span><span style="color: #DD0000">"{'Organization': 'PHP Documentation Team'}"</span><span style="color: #007700">;<br /><br /><br />foreach (</span><span style="color: #0000BB">$json </span><span style="color: #007700">as </span><span style="color: #0000BB">$string</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">'Decoding: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">$string</span><span style="color: #007700">;<br /> </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 /> switch (</span><span style="color: #0000BB">json_last_error</span><span style="color: #007700">()) {<br /> case </span><span style="color: #0000BB">JSON_ERROR_NONE</span><span style="color: #007700">:<br /> echo </span><span style="color: #DD0000">' - No errors'</span><span style="color: #007700">;<br /> break;<br /> case </span><span style="color: #0000BB">JSON_ERROR_DEPTH</span><span style="color: #007700">:<br /> echo </span><span style="color: #DD0000">' - Maximum stack depth exceeded'</span><span style="color: #007700">;<br /> break;<br /> case </span><span style="color: #0000BB">JSON_ERROR_STATE_MISMATCH</span><span style="color: #007700">:<br /> echo </span><span style="color: #DD0000">' - Underflow or the modes mismatch'</span><span style="color: #007700">;<br /> break;<br /> case </span><span style="color: #0000BB">JSON_ERROR_CTRL_CHAR</span><span style="color: #007700">:<br /> echo </span><span style="color: #DD0000">' - Unexpected control character found'</span><span style="color: #007700">;<br /> break;<br /> case </span><span style="color: #0000BB">JSON_ERROR_SYNTAX</span><span style="color: #007700">:<br /> echo </span><span style="color: #DD0000">' - Syntax error, malformed JSON'</span><span style="color: #007700">;<br /> break;<br /> case </span><span style="color: #0000BB">JSON_ERROR_UTF8</span><span style="color: #007700">:<br /> echo </span><span style="color: #DD0000">' - Malformed UTF-8 characters, possibly incorrectly encoded'</span><span style="color: #007700">;<br /> break;<br /> default:<br /> echo </span><span style="color: #DD0000">' - Unknown error'</span><span style="color: #007700">;<br /> break;<br /> }<br /><br /> echo </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>
|
||
Decoding: {"Organization": "PHP Documentation Team"} - No errors
|
||
Decoding: {'Organization': 'PHP Documentation Team'} - 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"><?php<br /></span><span style="color: #FF8000">// 无效的 UTF8 序列<br /></span><span style="color: #0000BB">$text </span><span style="color: #007700">= </span><span style="color: #DD0000">"\xB1\x31"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$json </span><span style="color: #007700">= </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 </span><span style="color: #007700">= </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">, </span><span style="color: #0000BB">$error </span><span style="color: #007700">=== </span><span style="color: #0000BB">JSON_ERROR_UTF8</span><span style="color: #007700">);<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>
|
||
string(4) "null"
|
||
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> |