2006-08-02 15:43:50 +00:00
|
|
|
<?xml version="1.0" encoding="iso-8859-1"?>
|
2009-07-11 07:50:41 +00:00
|
|
|
<!-- $Revision$ -->
|
2007-06-20 22:25:43 +00:00
|
|
|
<refentry xml:id="function.json-decode" xmlns="http://docbook.org/ns/docbook">
|
2006-08-02 15:43:50 +00:00
|
|
|
<refnamediv>
|
|
|
|
<refname>json_decode</refname>
|
2008-04-23 17:58:34 +00:00
|
|
|
<refpurpose>Decodes a JSON string</refpurpose>
|
2006-08-02 15:43:50 +00:00
|
|
|
</refnamediv>
|
|
|
|
|
|
|
|
<refsect1 role="description">
|
|
|
|
&reftitle.description;
|
|
|
|
<methodsynopsis>
|
|
|
|
<type>mixed</type><methodname>json_decode</methodname>
|
|
|
|
<methodparam><type>string</type><parameter>json</parameter></methodparam>
|
2009-01-12 15:45:03 +00:00
|
|
|
<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>
|
2006-08-02 15:43:50 +00:00
|
|
|
</methodsynopsis>
|
|
|
|
<para>
|
2008-04-23 17:58:34 +00:00
|
|
|
Takes a JSON encoded string and converts it into a PHP variable.
|
2006-08-02 15:43:50 +00:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="parameters">
|
|
|
|
&reftitle.parameters;
|
|
|
|
<para>
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term><parameter>json</parameter></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2008-04-23 17:58:34 +00:00
|
|
|
The <parameter>json</parameter> <type>string</type> being decoded.
|
2006-08-02 15:43:50 +00:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
|
|
|
<term><parameter>assoc</parameter></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2008-04-23 17:58:34 +00:00
|
|
|
When &true;, returned <type>object</type>s will be converted into
|
|
|
|
associative <type>array</type>s.
|
2006-08-02 15:43:50 +00:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2009-01-12 15:45:03 +00:00
|
|
|
<varlistentry>
|
|
|
|
<term><parameter>depth</parameter></term>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2009-07-27 03:31:00 +00:00
|
|
|
User specified recursion depth.
|
2009-01-12 15:45:03 +00:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2006-08-02 15:43:50 +00:00
|
|
|
</variablelist>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="returnvalues">
|
|
|
|
&reftitle.returnvalues;
|
|
|
|
<para>
|
2008-04-23 17:58:34 +00:00
|
|
|
Returns an <type>object</type> or if the optional
|
|
|
|
<parameter>assoc</parameter> parameter is &true;, an associative
|
2009-08-03 12:15:33 +00:00
|
|
|
<type>array</type> is instead returned. &null; is returned if the
|
|
|
|
<parameter>json</parameter> cannot be decoded or if the encoded
|
|
|
|
data is deeper than the recursion limit.
|
2006-08-02 15:43:50 +00:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="examples">
|
|
|
|
&reftitle.examples;
|
|
|
|
<para>
|
|
|
|
<example>
|
2008-04-23 17:58:34 +00:00
|
|
|
<title><function>json_decode</function> examples</title>
|
2006-08-02 15:43:50 +00:00
|
|
|
<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>
|
2008-01-23 22:05:59 +00:00
|
|
|
<example>
|
2008-07-25 08:48:04 +00:00
|
|
|
<title>Another example</title>
|
2008-01-23 22:05:59 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
|
|
|
|
$json = '{"foo-bar": 12345}';
|
|
|
|
|
|
|
|
$obj = json_decode($json);
|
|
|
|
print $obj->{'foo-bar'}; // 12345
|
|
|
|
|
2008-09-10 01:51:18 +00:00
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</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
|
|
|
|
|
2008-01-23 22:05:59 +00:00
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
2009-08-03 12:15:33 +00:00
|
|
|
<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.
|
|
|
|
$json_errors = array(
|
|
|
|
JSON_ERROR_NONE => 'No error has occurred',
|
|
|
|
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
|
|
|
|
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
|
|
|
|
JSON_ERROR_SYNTAX => 'Syntax error',
|
|
|
|
);
|
|
|
|
|
|
|
|
// 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 : No error has occurred
|
|
|
|
|
|
|
|
NULL
|
|
|
|
Last error : The maximum stack depth has been exceeded
|
|
|
|
]]>
|
|
|
|
</screen>
|
|
|
|
</example>
|
2006-08-02 15:43:50 +00:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
2007-05-25 19:56:26 +00:00
|
|
|
<refsect1 role="notes">
|
|
|
|
&reftitle.notes;
|
2008-09-10 01:51:18 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
The JSON spec is not JavaScript, but a subset of JavaScript.
|
|
|
|
</para>
|
|
|
|
</note>
|
2009-08-03 12:15:33 +00:00
|
|
|
<note>
|
2007-05-25 19:56:26 +00:00
|
|
|
<para>
|
2009-08-03 12:15:33 +00:00
|
|
|
In the event of a failure to decode, <function>json_last_error</function>
|
|
|
|
can be used to determine the exact nature of the error.
|
2007-05-25 19:56:26 +00:00
|
|
|
</para>
|
2009-08-03 12:15:33 +00:00
|
|
|
</note>
|
2007-05-25 19:56:26 +00:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="changelog">
|
|
|
|
&reftitle.changelog;
|
|
|
|
<para>
|
|
|
|
<informaltable>
|
|
|
|
<tgroup cols="2">
|
|
|
|
<thead>
|
|
|
|
<row>
|
|
|
|
<entry>&Version;</entry>
|
|
|
|
<entry>&Description;</entry>
|
|
|
|
</row>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2009-06-23 19:41:31 +00:00
|
|
|
<row>
|
|
|
|
<entry>5.3.0</entry>
|
2009-07-27 03:31:00 +00:00
|
|
|
<entry>Added the optional <parameter>depth</parameter>. The default recursion depth was increased from 128 to 512</entry>
|
2009-06-23 19:41:31 +00:00
|
|
|
</row>
|
2007-05-25 19:56:26 +00:00
|
|
|
<row>
|
|
|
|
<entry>5.2.3</entry>
|
2008-04-23 17:58:34 +00:00
|
|
|
<entry>The nesting limit was increased from 20 to 128</entry>
|
2007-05-25 19:56:26 +00:00
|
|
|
</row>
|
|
|
|
</tbody>
|
|
|
|
</tgroup>
|
|
|
|
</informaltable>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
2006-08-02 15:43:50 +00:00
|
|
|
<refsect1 role="seealso">
|
|
|
|
&reftitle.seealso;
|
|
|
|
<para>
|
|
|
|
<simplelist>
|
|
|
|
<member><function>json_encode</function></member>
|
2009-08-03 12:15:33 +00:00
|
|
|
<member><function>json_last_error</function></member>
|
2006-08-02 15:43:50 +00:00
|
|
|
</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
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2006-08-02 15:43:50 +00:00
|
|
|
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
|
|
|
|
-->
|