mirror of
https://github.com/php/doc-ja.git
synced 2026-03-30 11:02:18 +02:00
git-svn-id: https://svn.php.net/repository/phpdoc/ja/trunk@330256 c90b9560-bf6c-de11-be94-00142212c4b1
331 lines
9.4 KiB
XML
331 lines
9.4 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: f621ea25b00e93f645e6e663ce448cafdd85f45f Maintainer: takagi Status: ready -->
|
|
<refentry xml:id="function.json-encode" xmlns="http://docbook.org/ns/docbook">
|
|
<refnamediv>
|
|
<refname>json_encode</refname>
|
|
<refpurpose>値を JSON 形式にして返す</refpurpose>
|
|
</refnamediv>
|
|
|
|
<refsect1 role="description">
|
|
&reftitle.description;
|
|
<methodsynopsis>
|
|
<type>string</type><methodname>json_encode</methodname>
|
|
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
|
|
<methodparam choice="opt"><type>int</type><parameter>options</parameter><initializer>0</initializer></methodparam>
|
|
<methodparam choice="opt"><type>int</type><parameter>depth</parameter><initializer>512</initializer></methodparam>
|
|
</methodsynopsis>
|
|
<para>
|
|
<parameter>value</parameter> を JSON 形式にした文字列を返します。
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="parameters">
|
|
&reftitle.parameters;
|
|
<para>
|
|
<variablelist>
|
|
<varlistentry>
|
|
<term><parameter>value</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
エンコードする値。
|
|
<type>resource</type> 型以外の任意の型を指定できます。
|
|
</para>
|
|
<para>
|
|
この関数は、UTF-8 エンコードされたデータでのみ動作します。
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
<varlistentry>
|
|
<term><parameter>options</parameter></term>
|
|
<listitem>
|
|
<para>
|
|
<constant>JSON_HEX_QUOT</constant>,
|
|
<constant>JSON_HEX_TAG</constant>,
|
|
<constant>JSON_HEX_AMP</constant>,
|
|
<constant>JSON_HEX_APOS</constant>,
|
|
<constant>JSON_NUMERIC_CHECK</constant>,
|
|
<constant>JSON_PRETTY_PRINT</constant>,
|
|
<constant>JSON_UNESCAPED_SLASHES</constant>,
|
|
<constant>JSON_FORCE_OBJECT</constant>,
|
|
<constant>JSON_UNESCAPED_UNICODE</constant>
|
|
からなるビットマスク。
|
|
各定数の意味については
|
|
<link linkend="json.constants">JSON 定数のページ</link>
|
|
に説明があります。
|
|
</para>
|
|
</listitem>
|
|
</varlistentry>
|
|
</variablelist>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="returnvalues">
|
|
&reftitle.returnvalues;
|
|
<para>
|
|
成功した場合に、JSON エンコードされた文字列を返します。
|
|
&return.falseforfailure;。
|
|
</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>5.5.0</entry>
|
|
<entry>
|
|
パラメータ <parameter>depth</parameter> が追加されました。
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>5.4.0</entry>
|
|
<entry>
|
|
<parameter>option</parameter> に
|
|
<constant>JSON_PRETTY_PRINT</constant>、<constant>JSON_UNESCAPED_SLASHES</constant> および <constant>JSON_UNESCAPED_UNICODE</constant>
|
|
が追加されました。
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>5.3.3</entry>
|
|
<entry>
|
|
<parameter>option</parameter> に <constant>JSON_NUMERIC_CHECK</constant> が追加されました。
|
|
</entry>
|
|
</row>
|
|
<row>
|
|
<entry>5.3.0</entry>
|
|
<entry>
|
|
<parameter>options</parameter> パラメータが追加されました。
|
|
</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title><function>json_encode</function> の例</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
|
|
|
|
echo json_encode($arr);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
{"a":1,"b":2,"c":3,"d":4,"e":5}
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
|
|
<example>
|
|
<title>
|
|
<function>json_encode</function> で、利用中のいくつかのオプションを表示する例
|
|
</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
$a = array('<foo>',"'bar'",'"baz"','&blong&', "\xc3\xa9");
|
|
|
|
echo "Normal: ", json_encode($a), "\n";
|
|
echo "Tags: ", json_encode($a, JSON_HEX_TAG), "\n";
|
|
echo "Apos: ", json_encode($a, JSON_HEX_APOS), "\n";
|
|
echo "Quot: ", json_encode($a, JSON_HEX_QUOT), "\n";
|
|
echo "Amp: ", json_encode($a, JSON_HEX_AMP), "\n";
|
|
echo "Unicode: ", json_encode($a, JSON_UNESCAPED_UNICODE), "\n";
|
|
echo "All: ", json_encode($a, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE), "\n\n";
|
|
|
|
$b = array();
|
|
|
|
echo "Empty array output as array: ", json_encode($b), "\n";
|
|
echo "Empty array output as object: ", json_encode($b, JSON_FORCE_OBJECT), "\n\n";
|
|
|
|
$c = array(array(1,2,3));
|
|
|
|
echo "Non-associative array output as array: ", json_encode($c), "\n";
|
|
echo "Non-associative array output as object: ", json_encode($c, JSON_FORCE_OBJECT), "\n\n";
|
|
|
|
$d = array('foo' => 'bar', 'baz' => 'long');
|
|
|
|
echo "Associative array always output as object: ", json_encode($d), "\n";
|
|
echo "Associative array always output as object: ", json_encode($d, JSON_FORCE_OBJECT), "\n\n";
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
Normal: ["<foo>","'bar'","\"baz\"","&blong&","\u00e9"]
|
|
Tags: ["\u003Cfoo\u003E","'bar'","\"baz\"","&blong&","\u00e9"]
|
|
Apos: ["<foo>","\u0027bar\u0027","\"baz\"","&blong&","\u00e9"]
|
|
Quot: ["<foo>","'bar'","\u0022baz\u0022","&blong&","\u00e9"]
|
|
Amp: ["<foo>","'bar'","\"baz\"","\u0026blong\u0026","\u00e9"]
|
|
Unicode: ["<foo>","'bar'","\"baz\"","&blong&","é"]
|
|
All: ["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026","é"]
|
|
|
|
Empty array output as array: []
|
|
Empty array output as object: {}
|
|
|
|
Non-associative array output as array: [[1,2,3]]
|
|
Non-associative array output as object: {"0":{"0":1,"1":2,"2":3}}
|
|
|
|
Associative array always output as object: {"foo":"bar","baz":"long"}
|
|
Associative array always output as object: {"foo":"bar","baz":"long"}
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
<example>
|
|
<title>シーケンシャルな配列とそうでない配列の例</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
echo "Sequential array".PHP_EOL;
|
|
$sequential = array("foo", "bar", "baz", "blong");
|
|
var_dump(
|
|
$sequential,
|
|
json_encode($sequential)
|
|
);
|
|
|
|
echo PHP_EOL."Non-sequential array".PHP_EOL;
|
|
$nonsequential = array(1=>"foo", 2=>"bar", 3=>"baz", 4=>"blong");
|
|
var_dump(
|
|
$nonsequential,
|
|
json_encode($nonsequential)
|
|
);
|
|
|
|
echo PHP_EOL."Sequential array with one key unset".PHP_EOL;
|
|
unset($sequential[1]);
|
|
var_dump(
|
|
$sequential,
|
|
json_encode($sequential)
|
|
);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
Sequential array
|
|
array(4) {
|
|
[0]=>
|
|
string(3) "foo"
|
|
[1]=>
|
|
string(3) "bar"
|
|
[2]=>
|
|
string(3) "baz"
|
|
[3]=>
|
|
string(5) "blong"
|
|
}
|
|
string(27) "["foo","bar","baz","blong"]"
|
|
|
|
Non-sequential array
|
|
array(4) {
|
|
[1]=>
|
|
string(3) "foo"
|
|
[2]=>
|
|
string(3) "bar"
|
|
[3]=>
|
|
string(3) "baz"
|
|
[4]=>
|
|
string(5) "blong"
|
|
}
|
|
string(43) "{"1":"foo","2":"bar","3":"baz","4":"blong"}"
|
|
|
|
Sequential array with one key unset
|
|
array(3) {
|
|
[0]=>
|
|
string(3) "foo"
|
|
[2]=>
|
|
string(3) "baz"
|
|
[3]=>
|
|
string(5) "blong"
|
|
}
|
|
string(33) "{"0":"foo","2":"baz","3":"blong"}"
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
</refsect1>
|
|
|
|
<refsect1 role="notes">
|
|
&reftitle.notes;
|
|
<note>
|
|
<para>
|
|
エンコードに失敗した場合は、<function>json_last_error</function>
|
|
を使ってエラーの内容を調べることができます。
|
|
</para>
|
|
</note>
|
|
<note>
|
|
<para>
|
|
配列をエンコードする場合、もし配列のキーが 0 からはじまる連続した数値でなければ、
|
|
すべてのキーを文字列としてエンコードします。
|
|
そして、個々のキー/値のペアを明示的に指定します。
|
|
</para>
|
|
</note>
|
|
<note>
|
|
<para>
|
|
JSON エンコーダーのリファレンス実装と同様、
|
|
<type>string</type> や <type>integer</type>、
|
|
<type>float</type> そして <type>boolean</type> を入力として渡したときに
|
|
<function>json_encode</function> が生成する JSON
|
|
はシンプルな値 (オブジェクトでもないし配列でもないもの) となります。
|
|
大半のエンコーダーはこういった値を妥当な JSON として扱いますが、中にはそうでないものもあります。
|
|
そのため、現時点ではこの仕様は不明瞭です。
|
|
</para>
|
|
<para>
|
|
要するに、<function>json_encode</function> で生成した JSON
|
|
が、あなたの使おうとしているデコーダーでデコードできるかどうかを常に確認する必要があるということです。
|
|
</para>
|
|
</note>
|
|
</refsect1>
|
|
|
|
<refsect1 role="seealso">
|
|
&reftitle.seealso;
|
|
<para>
|
|
<simplelist>
|
|
<member><interfacename>JsonSerializable</interfacename></member>
|
|
<member><function>json_decode</function></member>
|
|
<member><function>json_last_error</function></member>
|
|
<member><function>serialize</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
|
|
-->
|