1
0
mirror of https://github.com/php/doc-zh.git synced 2026-04-24 00:28:08 +02:00
Files
2024-08-18 19:59:52 +08:00

351 lines
9.1 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 9f2e30a00afda6d6b6a3e19b13956150c2eaf2c1 Maintainer: jhdxr Status: ready -->
<!-- CREDITS: Luffy, mowangjuanzi -->
<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> <type>string</type> 格式的字符串。
</para>
<para>
这个函数仅能处理 UTF-8 编码的数据。
</para>
&json.implementation.superset;
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>associative</parameter></term>
<listitem>
<para>
当为 &true; 时,JSON 对象将返回关联 &array;;当为 &false; 时,JSON 对象将返回
&object;。当为 &null; 时,JSON 对象将返回关联 &array;&object;,这取决于是否在
<parameter>flags</parameter> 中设置 <constant>JSON_OBJECT_AS_ARRAY</constant>
</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 constants</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> 允许为 null。
</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>
An empty JSON key ("") can be encoded to the empty object property
instead of using a key with value <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>Accessing invalid object properties</title>
<simpara>
Accessing elements within an object that contain characters not
permitted under PHP's naming convention (e.g. the hyphen) can be
accomplished by encapsulating the element name within braces and the apostrophe.
</simpara>
<programlisting role="php">
<![CDATA[
<?php
$json = '{"foo-bar": 12345}';
$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345
?>
]]>
</programlisting>
</example>
<example>
<title>common mistakes using <function>json_decode</function></title>
<programlisting role="php">
<![CDATA[
<?php
// the following strings are valid JavaScript but not valid JSON
// the name and value must be enclosed in double quotes
// single quotes are not valid
$bad_json = "{ 'bar': 'baz' }";
json_decode($bad_json); // null
// the name must be enclosed in double quotes
$bad_json = '{ bar: "baz" }';
json_decode($bad_json); // null
// trailing commas are not allowed
$bad_json = '{ bar: "baz", }';
json_decode($bad_json); // null
?>
]]>
</programlisting>
</example>
<example>
<title><parameter>depth</parameter> errors</title>
<programlisting role="php">
<![CDATA[
<?php
// Encode some data with a maximum depth of 4 (array -> array -> array -> string)
$json = json_encode(
array(
1 => array(
'English' => array(
'One',
'January'
),
'French' => array(
'Une',
'Janvier'
)
)
)
);
// Show the errors for different depths.
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> of large integers</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>
The JSON spec is not JavaScript, but a subset of JavaScript.
</para>
</note>
<note>
<para>
In the event of a failure to decode, <function>json_last_error</function>
can be used to determine the exact nature of the error.
</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
-->