php-doc-en/reference/json/functions/json-decode.xml
George Peter Banyard 40352bfbde Add JSON constants to changelog.
Fixes:
Doc Bug #77798
Doc Bug #77811
Doc Bug #77881

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@347250 c90b9560-bf6c-de11-be94-00142212c4b1
2019-04-16 18:31:50 +00:00

406 lines
9.8 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="function.json-decode" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>json_decode</refname>
<refpurpose>Decodes a JSON string</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>
Takes a JSON encoded string and converts it into a PHP variable.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>json</parameter></term>
<listitem>
<para>
The <parameter>json</parameter> <type>string</type> being decoded.
</para>
<para>
This function only works with UTF-8 encoded strings.
</para>
&json.implementation.superset;
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>assoc</parameter></term>
<listitem>
<para>
When &true;, returned &object;s will be converted into
associative &array;s.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>depth</parameter></term>
<listitem>
<para>
User specified recursion depth.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>options</parameter></term>
<listitem>
<para>
Bitmask of
<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>.
The behaviour of these constants is described on the
<link linkend="json.constants">JSON constants</link> page.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns the value encoded in <parameter>json</parameter> in appropriate
PHP type. Values <literal>true</literal>, <literal>false</literal> and
<literal>null</literal> are returned as &true;, &false; and &null;
respectively. &null; is returned if the <parameter>json</parameter> cannot
be decoded or if the encoded data is deeper than the recursion limit.
</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>
<constant>JSON_THROW_ON_ERROR</constant>
<parameter>options</parameter> was added.
</entry>
</row>
<row>
<entry>7.2.0</entry>
<entry>
<constant>JSON_INVALID_UTF8_IGNORE</constant>, and
<constant>JSON_INVALID_UTF8_SUBSTITUTE</constant>
<parameter>options</parameter> were added.
</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>
<row>
<entry>7.0.0</entry>
<entry>
Rejected RFC 7159 incompatible number formats - top level
(07, 0xff, .1, -.1) and all levels ([1.], [1.e1])
</entry>
</row>
<row>
<entry>7.0.0</entry>
<entry>
An empty PHP string or value that after casting to string is an empty
string (<literal>NULL</literal>, <literal>FALSE</literal>) results
in JSON syntax error.
</entry>
</row>
<row>
<entry>5.6.0</entry>
<entry>
Invalid non-lowercased variants of the <literal>true</literal>,
<literal>false</literal> and <literal>null</literal> literals are no
longer accepted as valid input, and will generate warnings.
</entry>
</row>
<row>
<entry>5.4.0</entry>
<entry>
<constant>JSON_BIGINT_AS_STRING</constant>, and
<constant>JSON_OBJECT_AS_ARRAY</constant>
<parameter>options</parameter> were added.
</entry>
</row>
<row>
<entry>5.4.0</entry>
<entry>
The <parameter>options</parameter> parameter was added.
</entry>
</row>
<row>
<entry>5.3.0</entry>
<entry>
Added the optional <parameter>depth</parameter>. The default
recursion depth was increased from 128 to 512
</entry>
</row>
<row>
<entry>5.2.3</entry>
<entry>
The nesting limit was increased from 20 to 128
</entry>
</row>
<row>
<entry>5.2.1</entry>
<entry>
Added support for JSON decoding of basic types.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title><function>json_decode</function> examples</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 the data.
$json = json_encode(
array(
1 => array(
'English' => array(
'One',
'January'
),
'French' => array(
'Une',
'Janvier'
)
)
)
);
// Define the errors.
$constants = get_defined_constants(true);
$json_errors = array();
foreach ($constants["json"] as $name => $value) {
if (!strncmp($name, "JSON_ERROR_", 11)) {
$json_errors[$value] = $name;
}
}
// Show the errors for different depths.
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> 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>
</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
-->