note 88889 rejected from function.json-decode by danbrown
| From: | danbrown@php.net | Date: | Thu, 12 Feb 2009 22:13:19 +0000 |
| Subject: | note 88889 rejected from function.json-decode by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-150594@lists.php.net to get a copy of this message | ||
Note Submitter: Erman Kadir Kahraman(erman at kahraman dot org)
----
There are error in the preg_replace line:(remove the space)
$json = preg_replace('/(^|,)([\\s\\t]*)([^:]*) (([\\s\\t]*)):(([\\s\\t]*))/s',
'$1"$3"$4:', trim($json));
change to
$json = preg_replace('/(^|,)([\\s\\t]*)([^:]*)(([\\s\\t]*)):(([\\s\\t]*))/s',
'$1"$3"$4:', trim($json));
And
if try json like this :
{
test:{t:1,j:0,q:1,w:0,c:1,l:20,s:8},
second:{t:1,j:0,q:1,w:0,c:1,l:50,s:50},
another:{t:2,j:0,q:1,w:0}
}
it is not work.........
json string change to :
{
"test":{t:1,"j":0,"q":1,"w":0,"c":1,"l":20,"s":8},
"second":{t:1,"j":0,"q":1,"w":0,"c":1,"l":50,"s":50},
"another":{t:2,"j":0,"q":1,"w":0}
}
.....{t:1,........"second":{t:1,...........,"another":{t:2,.......
must be
.....{"t":1,......"second":{"t":1,.........,"another":{"t":2,.......
if regex expression change to
$json = preg_replace('/([{,])([\n\t\s]*)([a-zA-Z0-9\s]+)[\n\s\t]*:/',
'$1"$3":', trim($json));
or
$json = preg_replace('/([{,])([\n\t\s]*)([^:]+)[\n\s\t]*:/',
'$1"$3":', trim($json));
and
not need to remove curly brackets
this function is work if change this way
function is change to :
function json_code ($json) {
$json = preg_replace('/([{,])([\n\t\s]*)([a-zA-Z0-9\s]+)[\n\s\t]*:/',
'$1"$3":', trim($json));
return json_decode($json, true);
}