php-doc-en/reference/json/functions/json-decode.xml
2009-07-27 03:31:00 +00:00

214 lines
4.8 KiB
XML

<?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.
</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>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
<note>
<para>
The JSON spec is not JavaScript, but a subset of JavaScript.
</para>
</note>
<caution>
<para>
This function will return &false; if the JSON encoded data is deeper than
the recursion depth.
</para>
</caution>
</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>
</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:"../../../../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
-->