|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2013-05-30 10:06 UTC] zhaoyong dot lc at gmail dot com
 Description:
------------
I used json_decode to parse much more json string from fastjson library in Java,I 
found some errors which was JSON_ERROR_CTRL_CHAR or JSON_ERROR_SYNTAX always. Some 
special chars were found could cause these wrongs, so I replaced these chars to 
empty then It's work well.Code below have a control character after 'abc' which 
equals ascii 3, output of this code is Null. ascii 0 to 9 also produce wrong.
php code:
<?php
$str = "{'a':'abc'}";
var_dump(json_decode($str));
output: null
Expected result:
----------------
{"a":"abc"}
Actual result:
--------------
null
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Thu Oct 30 18:00:02 2025 UTC | 
$str = '{"a":"abc"}';JSON_ERROR_CTRL_CHAR seems the expected behavior as control char need to be escaped. $ php -r 'var_dump(json_decode("{\"ab\":\"abc\u0003\"}"));' class stdClass#1 (1) { public $ab => string(4) "abc" }Control char need to be escaped in specification of json, however, It restrict data exchange between java and php in json, web don't need restriction so strict. Meanwhile, If we can encode string that contain control chars to json in unicode, why we can't decode string that contains too. $arr = array("","","","",""); // some control chars in it $json = json_encode($arr); var_dump(json_decode($json), json_encode($arr));