1
0
mirror of https://github.com/php/doc-ja.git synced 2026-03-24 07:02:08 +01:00
Files
archived-doc-ja/reference/json/functions/json-decode.xml
2024-07-27 21:02:50 +09:00

372 lines
10 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 9f2e30a00afda6d6b6a3e19b13956150c2eaf2c1 Maintainer: takagi Status: ready -->
<!-- Credits: mumumu -->
<refentry xml:id="function.json-decode" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>json_decode</refname>
<refpurpose>JSON 文字列をデコードする</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>mixed</type><methodname>json_decode</methodname>
<methodparam><type>string</type><parameter>json</parameter></methodparam>
<methodparam choice="opt"><type class="union"><type>bool</type><type>null</type></type><parameter>associative</parameter><initializer>&null;</initializer></methodparam>
<methodparam choice="opt"><type>int</type><parameter>depth</parameter><initializer>512</initializer></methodparam>
<methodparam choice="opt"><type>int</type><parameter>flags</parameter><initializer>0</initializer></methodparam>
</methodsynopsis>
<para>
JSON エンコードされた文字列を受け取り、それを
PHP の値に変換します。
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>json</parameter></term>
<listitem>
<para>
デコード対象となる <parameter>json</parameter> 文字列。
</para>
<para>
この関数は UTF-8 でエンコードされた文字列でのみ動作します。
</para>
&json.implementation.superset;
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>associative</parameter></term>
<listitem>
<para>
&true; の場合、返されるオブジェクトは連想配列形式になります。
&false; の場合、返されるオブジェクトは &object; になります。
&null; の場合、返されるオブジェクトは <parameter>flags</parameter>
<constant>JSON_OBJECT_AS_ARRAY</constant> が設定されているかどうかによって、
連想配列形式か &object; かが変化します。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>depth</parameter></term>
<listitem>
<para>
デコードするデータのネストの深さの最大値。
この値は <literal>0</literal> より大きく、
<literal>2147483647</literal> 以下でなければいけません。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>flags</parameter></term>
<listitem>
<para>
<constant>JSON_BIGINT_AS_STRING</constant>,
<constant>JSON_INVALID_UTF8_IGNORE</constant>,
<constant>JSON_INVALID_UTF8_SUBSTITUTE</constant>,
<constant>JSON_OBJECT_AS_ARRAY</constant>,
<constant>JSON_THROW_ON_ERROR</constant>.
からなるビットマスク。
各定数の意味については
<link linkend="json.constants">JSON 定数のページ</link>
に説明があります。
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
<parameter>json</parameter> でエンコードされたデータを、適切な PHP の型として返します。
クォートされていない <literal>true</literal><literal>false</literal> および
<literal>null</literal> はそれぞれ &true;&false;
そして &null; として返されます。
<parameter>json</parameter> のデコードに失敗したり
エンコードされたデータがネストの最大値を超えているなどの場合、&null; を返します。
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
<para>
<parameter>depth</parameter> が範囲外の値の場合、
PHP 8.0.0 以降では
<classname>ValueError</classname> がスローされます。
それより前のバージョンでは、
<constant>E_WARNING</constant> レベルの警告が発生していました。
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>7.3.0</entry>
<entry>
<parameter>flags</parameter> パラメータに
<constant>JSON_THROW_ON_ERROR</constant>
が追加されました。
</entry>
</row>
<row>
<entry>7.2.0</entry>
<entry>
<parameter>associative</parameter> は nullable になりました。
</entry>
</row>
<row>
<entry>7.2.0</entry>
<entry>
<parameter>flags</parameter> パラメータに
<constant>JSON_INVALID_UTF8_IGNORE</constant> および
<constant>JSON_INVALID_UTF8_SUBSTITUTE</constant> が追加されました。
</entry>
</row>
<row>
<entry>7.1.0</entry>
<entry>
空の JSONキー ("") は、
<literal>_empty_</literal> というキーではなく、
空のオブジェクトプロパティにエンコードされるようになりました。
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>json_decode</function> の例</title>
<programlisting role="php">
<![CDATA[
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
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)
}
]]>
</screen>
</example>
<example>
<title>無効なオブジェクトプロパティへのアクセス</title>
<simpara>
オブジェクトの中にある、
PHP の命名規約では使えない文字 (ハイフンなど) を含む要素にアクセスするには、
要素名を波括弧とアポストロフィで囲みます。
</simpara>
<programlisting role="php">
<![CDATA[
<?php
$json = '{"foo-bar": 12345}';
$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345
?>
]]>
</programlisting>
</example>
<example>
<title><function>json_decode</function> でのありがちな間違い</title>
<programlisting role="php">
<![CDATA[
<?php
// 以下の文字列は JavaScript としては有効ですが JSON としては無効です
// 名前と値はダブルクォートで囲む必要があります。
// シングルクォートは使えません
$bad_json = "{ 'bar': 'baz' }";
json_decode($bad_json); // null
// 名前をダブルクォートで囲まなければなりません
$bad_json = '{ bar: "baz" }';
json_decode($bad_json); // null
// 最後にカンマをつけてはいけません
$bad_json = '{ bar: "baz", }';
json_decode($bad_json); // null
?>
]]>
</programlisting>
</example>
<example>
<title><parameter>depth</parameter> エラー</title>
<programlisting role="php">
<![CDATA[
<?php
// データをエンコードします。ネストの深さは4です (array -> array -> array -> string)
$json = json_encode(
array(
1 => array(
'English' => array(
'One',
'January'
),
'French' => array(
'Une',
'Janvier'
)
)
)
);
// さまざまな深さのエラーを表示します
var_dump(json_decode($json, true, 4));
echo 'Last error: ', json_last_error_msg(), PHP_EOL, PHP_EOL;
var_dump(json_decode($json, true, 3));
echo 'Last error: ', json_last_error_msg(), PHP_EOL, PHP_EOL;
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
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: No error
NULL
Last error: Maximum stack depth exceeded
]]>
</screen>
</example>
<example>
<title><function>json_decode</function> で大きな整数値を扱う例</title>
<programlisting role="php">
<![CDATA[
<?php
$json = '{"number": 12345678901234567890}';
var_dump(json_decode($json));
var_dump(json_decode($json, false, 512, JSON_BIGINT_AS_STRING));
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
object(stdClass)#1 (1) {
["number"]=>
float(1.2345678901235E+19)
}
object(stdClass)#1 (1) {
["number"]=>
string(20) "12345678901234567890"
}
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<note>
<para>
JSON の仕様は JavaScript そのものではなく、JavaScript のサブセットです。
</para>
</note>
<note>
<para>
デコードに失敗した場合は、<function>json_last_error</function>
を使用すればエラーの正確な状態を知ることができます。
</para>
</note>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>json_encode</function></member>
<member><function>json_last_error</function></member>
<member><function>json_last_error_msg</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
-->