note 87776 deleted from function.json-decode by googleguy
| From: | googleguy@php.net | Date: | Wed, 16 Jan 2013 05:36:30 +0000 |
| Subject: | note 87776 deleted from function.json-decode by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193102@lists.php.net to get a copy of this message | ||
Note Submitter: jan at hooda dot de
----
This function will convert a "normal" json to an array.
<?php
function json_code ($json) {
//remove curly brackets to beware from regex errors
$json = substr($json, strpos($json,'{')+1, strlen($json));
$json = substr($json, 0, strrpos($json,'}'));
$json = preg_replace('/(^|,)([\\s\\t]*)([^:]*) (([\\s\\t]*)):(([\\s\\t]*))/s',
'$1"$3"$4:', trim($json));
return json_decode('{'.$json.'}', true);
}
$json_data = '{
a: 1,
b: 245,
c with whitespaces: "test me",
d: "function () { echo \"test\" }",
e: 5.66
}';
$jarr = json_code($json_data);
?>