note 92817 deleted from function.json-encode by cmb
| From: | cmb@php.net | Date: | Sun, 04 Nov 2018 09:56:20 +0000 |
| Subject: | note 92817 deleted from function.json-encode by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-209600@lists.php.net to get a copy of this message | ||
Note Submitter: http://mike.eire.ca/
----
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.