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.

Advertisements

About qianggan

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s