Fix JSON decoding syntax error

I got a long JSON string from DB, and try to decode the string using json_decode() in PHP, I get null, the empty object. But when I tested it in a JSON pretty page http://jsonprettyprint.com/, everything looks good.

It’s no single/double-quotes in the JSON string, and encoding in UT8, and no escaped characters as well. But it’s always shows 4 as json_last_error() result , which means syntax error.

Then I tried to filter invisible characters from JSON string, like below

for ($i = 0; $i <= 31; ++$i) {
	$history = str_replace(chr($i), "", $history);
}
$history = str_replace(chr(127), "", $history);
$history = json_decode($history);

Then the issue solved.

About qianggan

Sr. Software Engineer
This entry was posted in Computers and Internet, Laravel, Lumen, Uncategorized, Yii. Bookmark the permalink.

Leave a comment