mirror of
https://github.com/php/doc-ja.git
synced 2026-04-23 07:58:01 +02:00
251 lines
6.9 KiB
XML
251 lines
6.9 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: 7100979e254970cef72c6f91b32352e7c7898635 Maintainer: takagi Status: ready -->
|
|
<!-- Credits: mumumu -->
|
|
<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>
|
|
<constant>JSON_THROW_ON_ERROR</constant> を指定していなかった場合に、
|
|
直近の JSON エンコード/デコード/検証処理中に発生したエラー (もし存在すれば) を返します。
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
&no.function.parameters;
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
整数値を返します。これは、次の定数のいずれかとなります。
|
|
</para>
|
|
<table>
|
|
<title>JSON error codes</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>制御文字エラー。おそらくエンコーディングが違います</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></entry>
|
|
</row>
|
|
<row>
|
|
<entry><constant>JSON_ERROR_RECURSION</constant></entry>
|
|
<entry>エンコード対象の値に再帰参照が含まれています</entry>
|
|
<entry></entry>
|
|
</row>
|
|
<row>
|
|
<entry><constant>JSON_ERROR_INF_OR_NAN</constant></entry>
|
|
<entry>
|
|
エンコード対象の値に
|
|
<link linkend="language.types.float.nan"><constant>NAN</constant></link>
|
|
あるいは <link linkend="function.is-infinite"><constant>INF</constant></link>
|
|
が含まれています。
|
|
</entry>
|
|
<entry></entry>
|
|
</row>
|
|
<row>
|
|
<entry><constant>JSON_ERROR_UNSUPPORTED_TYPE</constant></entry>
|
|
<entry>エンコード不可能な型の値が渡されました</entry>
|
|
<entry></entry>
|
|
</row>
|
|
<row>
|
|
<entry><constant>JSON_ERROR_INVALID_PROPERTY_NAME</constant></entry>
|
|
<entry>エンコードできないプロパティ名が渡されました</entry>
|
|
<entry></entry>
|
|
</row>
|
|
<row>
|
|
<entry><constant>JSON_ERROR_UTF16</constant></entry>
|
|
<entry>おそらく正しくエンコードされていない、不正な UTF-16 文字</entry>
|
|
<entry></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
|
|
// 正しい json 文字列
|
|
$json[] = '{"Organization": "PHP Documentation Team"}';
|
|
|
|
// 間違った json 文字列で、構文エラーとなります
|
|
// ここでは、クォートに " ではなく ' を使用しています
|
|
$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> と <function>json_encode</function> の組み合わせ</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
// 無効な UTF8 シーケンス
|
|
$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>
|
|
<para>
|
|
<example>
|
|
<title><function>json_last_error</function> と <constant>JSON_THROW_ON_ERROR</constant> の組み合わせ</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
// JSON_ERROR_UTF8 が発生する、不正なUTF8シーケンス
|
|
json_encode("\xB1\x31");
|
|
|
|
// 次の文字列は、JSON error が発生しない
|
|
json_encode('okay', JSON_THROW_ON_ERROR);
|
|
|
|
// グローバルなエラー状態は、直前の json_encode() によって変更されない
|
|
var_dump(json_last_error() === JSON_ERROR_UTF8);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
bool(true)
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><function>json_last_error_msg</function></member>
|
|
<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
|
|
-->
|