note 95782 added to function.json-decode
| From: | colin dot mollenhour dot com at osu1 dot php dot net | Date: | Thu, 21 Jan 2010 18:51:32 +0000 |
| Subject: | note 95782 added to function.json-decode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-165175@lists.php.net to get a copy of this message | ||
For those of you wanting json_decode to be a little more lenient (more like Javascript), here is a
wrapper:
<?php
function json_decode_nice($json, $assoc = FALSE){
$json = str_replace(array("\n","\r"),"",$json);
$json =
preg_replace('/([{,])(\s*)([^"]+?)\s*:/','$1"$3":',$json);
return json_decode($json,$assoc);
}
?>
Some examples of accepted syntax:
<?php
$json = '{a:{b:"c",d:["e","f",0]}}';
$json =
'{
a : {
b : "c",
"d.e.f": "g"
}
}';
?>
If your content needs to have newlines, do this:
<?php
$string = "This
Text
Has
Newlines";
$json = '{withnewlines:'.json_encode($string).'}';
?>
Note: This does not fix trailing commas or single quotes.
----
Server IP: 64.71.164.2
Probable Submitter: 68.52.135.113
----
Manual Page -- http://www.php.net/manual/en/function.json-decode.php
Edit -- https://master.php.net/note/edit/95782
Del: integrated -- https://master.php.net/note/delete/95782/integrated
Del: useless -- https://master.php.net/note/delete/95782/useless
Del: bad code -- https://master.php.net/note/delete/95782/bad+code
Del: spam -- https://master.php.net/note/delete/95782/spam
Del: non-english -- https://master.php.net/note/delete/95782/non-english
Del: in docs -- https://master.php.net/note/delete/95782/in+docs
Del: other reasons-- https://master.php.net/note/delete/95782
Reject -- https://master.php.net/note/reject/95782
Search -- https://master.php.net/manage/user-notes.php