1
0
mirror of https://github.com/php/doc-zh.git synced 2026-03-24 15:12:20 +01:00
Files
archived-doc-zh/reference/json/functions/json-encode.xml
2024-08-18 19:59:52 +08:00

416 lines
12 KiB
XML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 9f2e30a00afda6d6b6a3e19b13956150c2eaf2c1 Maintainer: daijie Status: ready -->
<!-- CREDITS: mowangjuanzi, Luffy -->
<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 class="union"><type>string</type><type>false</type></type><methodname>json_encode</methodname>
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>flags</parameter><initializer>0</initializer></methodparam>
<methodparam choice="opt"><type>int</type><parameter>depth</parameter><initializer>512</initializer></methodparam>
</methodsynopsis>
<para>
返回字符串,包含了 <parameter>value</parameter> 值 JSON 形式的表示。如果参数是 &array;&object;,则会递归序列化。
</para>
<para>
如果要序列化的值是对象,则默认仅包含公开可见的属性,或者类可以实现 <interfacename>JsonSerializable</interfacename>
控制其值如何序列化为 <acronym>JSON</acronym>
</para>
<para>
编码受传入的 <parameter>flags</parameter> 参数影响,此外浮点值的编码依赖于 <link linkend="ini.serialize-precision">serialize_precision</link>
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>value</parameter></term>
<listitem>
<para>
待编码的 <parameter>value</parameter> ,除了 &resource;
类型之外,可以为任何数据类型。
</para>
<para>
所有字符串数据的编码必须是 UTF-8。
</para>
&json.implementation.superset;
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>flags</parameter></term>
<listitem>
<para>
由以下常量组成的二进制掩码:
<constant>JSON_FORCE_OBJECT</constant>
<constant>JSON_HEX_QUOT</constant>
<constant>JSON_HEX_TAG</constant>
<constant>JSON_HEX_AMP</constant>
<constant>JSON_HEX_APOS</constant>
<constant>JSON_INVALID_UTF8_IGNORE</constant>
<constant>JSON_INVALID_UTF8_SUBSTITUTE</constant>
<constant>JSON_NUMERIC_CHECK</constant>
<constant>JSON_PARTIAL_OUTPUT_ON_ERROR</constant>
<constant>JSON_PRESERVE_ZERO_FRACTION</constant>
<constant>JSON_PRETTY_PRINT</constant>
<constant>JSON_UNESCAPED_LINE_TERMINATORS</constant>
<constant>JSON_UNESCAPED_SLASHES</constant>
<constant>JSON_UNESCAPED_UNICODE</constant>
<constant>JSON_THROW_ON_ERROR</constant>
关于 JSON 常量详情参考
<link linkend="json.constants">JSON 常量</link>页面。
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>depth</parameter></term>
<listitem>
<para>
设置最大深度。 必须大于0。
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
成功则返回 JSON 编码的 &string; &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>7.3.0</entry>
<entry>
<parameter>flags</parameter> 参数新增
<constant>JSON_THROW_ON_ERROR</constant>
常量。
</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>
<parameter>flags</parameter> 参数新增
<constant>JSON_UNESCAPED_LINE_TERMINATORS</constant>
常量。
</entry>
</row>
<row>
<entry>7.1.0</entry>
<entry>
<type>float</type> 值进行编码时,使用
<link linkend="ini.serialize-precision">serialize_precision</link> 代替
<link linkend="ini.precision">precision</link>
</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> 函数中 flags 参数的用法</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>选项 JSON_NUMERIC_CHECK 例子</title>
<programlisting role="php">
<![CDATA[
<?php
echo "Strings representing numbers automatically turned into numbers".PHP_EOL;
$numbers = array('+123123', '-123123', '1.2e3', '0.00001');
var_dump(
$numbers,
json_encode($numbers, JSON_NUMERIC_CHECK)
);
echo "Strings containing improperly formatted numbers".PHP_EOL;
$strings = array('+a33123456789', 'a123');
var_dump(
$strings,
json_encode($strings, JSON_NUMERIC_CHECK)
);
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
Strings representing numbers automatically turned into numbers
array(4) {
[0]=>
string(7) "+123123"
[1]=>
string(7) "-123123"
[2]=>
string(5) "1.2e3"
[3]=>
string(7) "0.00001"
}
string(28) "[123123,-123123,1200,1.0e-5]"
Strings containing improperly formatted numbers
array(2) {
[0]=>
string(13) "+a33123456789"
[1]=>
string(4) "a123"
}
string(24) "["+a33123456789","a123"]"
]]>
</screen>
</example>
<example>
<title>连续与非连续数组示例</title>
<programlisting role="php">
<![CDATA[
<?php
echo "连续数组".PHP_EOL;
$sequential = array("foo", "bar", "baz", "blong");
var_dump(
$sequential,
json_encode($sequential)
);
echo PHP_EOL."非连续数组".PHP_EOL;
$nonsequential = array(1=>"foo", 2=>"bar", 3=>"baz", 4=>"blong");
var_dump(
$nonsequential,
json_encode($nonsequential)
);
echo PHP_EOL."删除一个连续数组值的方式产生的非连续数组".PHP_EOL;
unset($sequential[1]);
var_dump(
$sequential,
json_encode($sequential)
);
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
连续数组
array(4) {
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
[2]=>
string(3) "baz"
[3]=>
string(5) "blong"
}
string(27) "["foo","bar","baz","blong"]"
非连续数组
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"}"
删除一个连续数组值的方式产生的非连续数组
array(3) {
[0]=>
string(3) "foo"
[2]=>
string(3) "baz"
[3]=>
string(5) "blong"
}
string(33) "{"0":"foo","2":"baz","3":"blong"}"
]]>
</screen>
</example>
<example>
<title><constant>选项 JSON_PRESERVE_ZERO_FRACTION</constant> 的例子</title>
<programlisting role="php">
<![CDATA[
<?php
var_dump(json_encode(12.0, JSON_PRESERVE_ZERO_FRACTION));
var_dump(json_encode(12.0));
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string(4) "12.0"
string(2) "12"
]]>
</screen>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<note>
<para>
如果执行失败,可以通过 <function>json_last_error</function> 函数来获取详细错误信息。
</para>
</note>
<note>
<para>
如果要编码的数组的键不是从0开始的数字所有的键将会被当作字符串并明确声明为 key-value 对。
</para>
</note>
<note>
<para>
参考 JSON 解析器,如果指定简单的 <type>string</type><type>integer</type><type>float</type>
<type>boolean</type> 作为输入 <parameter>value</parameter><function>json_encode</function>
将生成简单的值的 JSON既不是对象又不是数组。虽然大多数解码器会接受这些值作为有效的
JSON但有些可能不会因为在这一点上规范是模棱两可的。
</para>
<para>
总而言之,应该测试下 JSON decoder 能否处理 <function>json_encode</function> 生成的数据。
</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>json_last_error_msg</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
-->