note 92817 added to function.json-encode
| From: | http://mike dot eire dot ca/ at osu1 dot php dot net | Date: | Mon, 10 Aug 2009 20:54:03 +0000 |
| Subject: | note 92817 added to function.json-encode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159291@lists.php.net to get a copy of this message | ||
Note that this function does not always produce legal JSON.
<?php
$json = json_encode('foo');
var_dump($json);
//string(5) ""foo""
$json = json_encode(23);
var_dump($json);
//string(2) "23"
?>
According to the JSON spec, only objects and arrays can be represented; the JSON_FORCE_OBJECT flag
available since PHP 5.3 does not change this behaviour. If you're using this to produce JSON
that will be exchanged with other systems, adjust your output accordingly.
<?php
$json = preg_replace('/^([^[{].*)$/', '[$1]', $json);
?>
The json_decode function accepts these JSON fragments without complaint.
----
Server IP: 66.207.199.35
Probable Submitter: 70.74.192.79
----
Manual Page -- http://www.php.net/manual/en/function.json-encode.php
Edit -- https://master.php.net/note/edit/92817
Del: integrated -- https://master.php.net/note/delete/92817/integrated
Del: useless -- https://master.php.net/note/delete/92817/useless
Del: bad code -- https://master.php.net/note/delete/92817/bad+code
Del: spam -- https://master.php.net/note/delete/92817/spam
Del: non-english -- https://master.php.net/note/delete/92817/non-english
Del: in docs -- https://master.php.net/note/delete/92817/in+docs
Del: other reasons-- https://master.php.net/note/delete/92817
Reject -- https://master.php.net/note/reject/92817
Search -- https://master.php.net/manage/user-notes.php