mirror of
https://github.com/php/doc-ja.git
synced 2026-03-30 02:52:08 +02:00
git-svn-id: https://svn.php.net/repository/phpdoc/ja/trunk@328741 c90b9560-bf6c-de11-be94-00142212c4b1
345 lines
8.6 KiB
XML
345 lines
8.6 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: 0d52021319573c6181f23955f71356b1e52b9570 Maintainer: takagi Status: ready -->
|
|
<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>bool</type><parameter>assoc</parameter><initializer>false</initializer></methodparam>
|
|
<methodparam choice="opt"><type>int</type><parameter>depth</parameter><initializer>512</initializer></methodparam>
|
|
<methodparam choice="opt"><type>int</type><parameter>options</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>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>assoc</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
&true; の場合は、返されるオブジェクトが連想配列形式になります。
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>depth</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
ユーザー指定の再帰の深さ。
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>options</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
JSON デコードオプションのビットマスク。現在サポートしているオプションは
|
|
<constant>JSON_BIGINT_AS_STRING</constant> のみです
|
|
(デフォルトでは、大きな整数値を float に変換します)。
|
|
</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="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
|
|
// データをエンコードします
|
|
$json = json_encode(
|
|
array(
|
|
1 => array(
|
|
'English' => array(
|
|
'One',
|
|
'January'
|
|
),
|
|
'French' => array(
|
|
'Une',
|
|
'Janvier'
|
|
)
|
|
)
|
|
)
|
|
);
|
|
|
|
// エラーを定義します
|
|
$constants = get_defined_constants(true);
|
|
$json_errors = array();
|
|
foreach ($constants["json"] as $name => $value) {
|
|
if (!strncmp($name, "JSON_ERROR_", 11)) {
|
|
$json_errors[$value] = $name;
|
|
}
|
|
}
|
|
|
|
// さまざまな深さのエラーを表示します
|
|
foreach (range(4, 3, -1) as $depth) {
|
|
var_dump(json_decode($json, true, $depth));
|
|
echo 'Last error: ', $json_errors[json_last_error()], 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: JSON_ERROR_NONE
|
|
|
|
NULL
|
|
Last error: JSON_ERROR_DEPTH
|
|
]]>
|
|
</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="changelog">
|
|
&reftitle.changelog;
|
|
<para>
|
|
<informaltable>
|
|
<tgroup cols="2">
|
|
<thead>
|
|
<row>
|
|
<entry>&Version;</entry>
|
|
<entry>&Description;</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>5.4.0</entry>
|
|
<entry>
|
|
<parameter>options</parameter> パラメータが追加されました。
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>5.3.0</entry>
|
|
<entry>オプションの <parameter>depth</parameter> が追加されました。デフォルトの再帰の深さが 128 から 512 に増えました。</entry>
|
|
</row>
|
|
<row>
|
|
<entry>5.2.3</entry>
|
|
<entry>ネストの制限が 20 から 128 に拡張されました。</entry>
|
|
</row>
|
|
<row>
|
|
<entry>5.2.1</entry>
|
|
<entry>
|
|
基本型の JSON デコードに対応しました。
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><function>json_encode</function></member>
|
|
<member><function>json_last_error</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
|
|
-->
|