Added an example showing some common problems people using JavaScript run into when using json_decode.

Also added a note stating that JSON is not the same as JavaScript in hopes that people will realize that the json_decode function is not buggy but instead following the JSON specification.


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@266054 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Herman J. Radtke III 2008-09-10 01:51:18 +00:00
parent 4a6e79e26a
commit 959be50e8b

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.7 $ -->
<!-- $Revision: 1.8 $ -->
<refentry xml:id="function.json-decode" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>json_decode</refname>
@ -100,6 +100,31 @@ $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>
@ -109,6 +134,11 @@ print $obj->{'foo-bar'}; // 12345
<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