<?xml version="1.0" encoding="iso-8859-1"?> <!-- $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> </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> </listitem> </varlistentry> <varlistentry> <term><parameter>assoc</parameter></term> <listitem> <para> When &true;, returned <type>object</type>s will be converted into associative <type>array</type>s. </para> </listitem> </varlistentry> <varlistentry> <term><parameter>depth</parameter></term> <listitem> <para> User specified recursion depth. </para> </listitem> </varlistentry> </variablelist> </para> </refsect1> <refsect1 role="returnvalues"> &reftitle.returnvalues; <para> Returns an <type>object</type> or if the optional <parameter>assoc</parameter> parameter is &true;, an associative <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. </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>Another example</title> <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. $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> </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="changelog"> &reftitle.changelog; <para> <informaltable> <tgroup cols="2"> <thead> <row> <entry>&Version;</entry> <entry>&Description;</entry> </row> </thead> <tbody> <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> </tbody> </tgroup> </informaltable> </para> </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 -->