[phpdoc-zh] r227 committed - [PHPDOC-ZH] [翻译] reference.json.json-last-error 函数

0 views
Skip to first unread message

phpd...@googlecode.com

unread,
May 15, 2012, 4:41:10 AM5/15/12
to phpd...@googlegroups.com
Revision: 227
Author: ice.shiny
Date: Tue May 15 01:40:51 2012
Log: [PHPDOC-ZH] [翻译] reference.json.json-last-error 函数
http://code.google.com/p/phpdoc-zh/source/detail?r=227

Added:
/trunk/xml/reference/json/functions/json-last-error.xml

=======================================
--- /dev/null
+++ /trunk/xml/reference/json/functions/json-last-error.xml Tue May 15
01:40:51 2012
@@ -0,0 +1,191 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- $Revision: 312208 $ -->
+<refentry xml:id="function.json-last-error"
xmlns="http://docbook.org/ns/docbook">
+ <refnamediv>
+ <refname>json_last_error</refname>
+ <refpurpose>返回最后发生的一个错误</refpurpose>
+ </refnamediv>
+
+ <refsect1 role="description">
+ &reftitle.description;
+ <methodsynopsis>
+ <type>int</type><methodname>json_last_error</methodname>
+ <void />
+ </methodsynopsis>
+ <para>
+ 返回JSON编码/解码中最后一次发生的错误(如果有的话)。
+ </para>
+ </refsect1>
+
+ <refsect1 role="parameters">
+ &reftitle.parameters;
+ &no.function.parameters;
+ </refsect1>
+
+ <refsect1 role="returnvalues">
+ &reftitle.returnvalues;
+ <para>
+ 返回一个 integer,是以下常量中的一个:
+ </para>
+ <table>
+ <title>JSON错误码</title>
+ <tgroup cols="2">
+ <thead>
+ <row>
+ <entry>常量名</entry>
+ <entry>含义</entry>
+ <entry>有效性</entry>
+ </row>
+ </thead>
+ <tbody>
+ <row>
+ <entry><constant>JSON_ERROR_NONE</constant></entry>
+ <entry>没有发生错误</entry>
+ <entry></entry>
+ </row>
+ <row>
+ <entry><constant>JSON_ERROR_DEPTH</constant></entry>
+ <entry>超过了最大堆栈深度。</entry>
+ <entry></entry>
+ </row>
+ <row>
+ <entry><constant>JSON_ERROR_STATE_MISMATCH</constant></entry>
+ <entry>无效或异常的JSON</entry>
+ <entry></entry>
+ </row>
+ <row>
+ <entry><constant>JSON_ERROR_CTRL_CHAR</constant></entry>
+ <entry>Control字符错误,也许使用了错误的编码。</entry>
+ <entry></entry>
+ </row>
+ <row>
+ <entry><constant>JSON_ERROR_SYNTAX</constant></entry>
+ <entry>语法错误</entry>
+ <entry></entry>
+ </row>
+ <row>
+ <entry><constant>JSON_ERROR_UTF8</constant></entry>
+ <entry>异常的 UTF-8 字符,也许是因为错误的编码</entry>
+ <entry>PHP 5.3.3</entry>
+ </row>
+ </tbody>
+ </tgroup>
+ </table>
+ </refsect1>
+
+ <refsect1 role="examples">
+ &reftitle.examples;
+ <para>
+ <example>
+ <title><function>json_last_error</function> 示例</title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+// A valid json string
+$json[] = '{"Organization": "PHP Documentation Team"}';
+
+// An invalid json string which will cause an syntax
+// error, in this case we used ' instead of " for quotation
+$json[] = "{'Organization': 'PHP Documentation Team'}";
+
+
+foreach ($json as $string) {
+ echo 'Decoding: ' . $string;
+ json_decode($string);
+
+ switch (json_last_error()) {
+ case JSON_ERROR_NONE:
+ echo ' - No errors';
+ break;
+ case JSON_ERROR_DEPTH:
+ echo ' - Maximum stack depth exceeded';
+ break;
+ case JSON_ERROR_STATE_MISMATCH:
+ echo ' - Underflow or the modes mismatch';
+ break;
+ case JSON_ERROR_CTRL_CHAR:
+ echo ' - Unexpected control character found';
+ break;
+ case JSON_ERROR_SYNTAX:
+ echo ' - Syntax error, malformed JSON';
+ break;
+ case JSON_ERROR_UTF8:
+ echo ' - Malformed UTF-8 characters, possibly incorrectly
encoded';
+ break;
+ default:
+ echo ' - Unknown error';
+ break;
+ }
+
+ echo PHP_EOL;
+}
+?>
+]]>
+ </programlisting>
+ &example.outputs;
+ <screen>
+<![CDATA[
+Decoding: {"Organization": "PHP Documentation Team"} - No errors
+Decoding: {'Organization': 'PHP Documentation Team'} - Syntax error,
malformed JSON
+]]>
+ </screen>
+ </example>
+ </para>
+ <para>
+ <example>
+ <title><function>json_last_error</function> with
<function>json_encode</function></title>
+ <programlisting role="php">
+<![CDATA[
+<?php
+// An invalid UTF8 sequence
+$text = "\xB1\x31";
+
+$json = json_encode($text);
+$error = json_last_error();
+
+var_dump($json, $error === JSON_ERROR_UTF8);
+?>
+]]>
+ </programlisting>
+ &example.outputs;
+ <screen>
+<![CDATA[
+string(4) "null"
+bool(true)
+]]>
+ </screen>
+ </example>
+ </para>
+ </refsect1>
+
+ <refsect1 role="seealso">
+ &reftitle.seealso;
+ <para>
+ <simplelist>
+ <member><function>json_decode</function></member>
+ <member><function>json_encode</function></member>
+ </simplelist>
+ </para>
+ </refsect1>
+</refentry>
+
+<!-- Keep this comment at the end of the file
+Local variables:
+mode: sgml
+sgml-omittag:t
+sgml-shorttag:t
+sgml-minimize-attributes:nil
+sgml-always-quote-attributes:t
+sgml-indent-step:1
+sgml-indent-data:t
+indent-tabs-mode:nil
+sgml-parent-document:nil
+sgml-default-dtd-file:"~/.phpdoc/manual.ced"
+sgml-exposed-tags:nil
+sgml-local-catalogs:nil
+sgml-local-ecat-files:nil
+End:
+vim600: syn=xml fen fdm=syntax fdl=2 si
+vim: et tw=78 syn=sgml
+vi: ts=1 sw=1
+-->
Reply all
Reply to author
Forward
0 new messages