Error description
In the PHP development process, when processing json strings, json_decode returns NULL, calling last_error returns 4 (JSON_ERROR_SYNTAX), but json strings can be correctly processed by other languages such as python, javascript or some online json parsers.
Diagnosis
There are several situations that generally cause php json_decode errors here:
1. The json string is read from a file and the character order mark (BOM) is not removed
2. json contains invisible characters, json_decode parsing errors
3. json object value is a single-quoted string
In particular, the third error is relatively hidden, the naked eye is often easy to ignore
Solution:
The following solutions are given for the above three cases
1.BOM Issue:
Open the file in binary mode and confirm whether there is a BOM. If so, remove the BOM before parsing. The following code takes UTF-8 as an example to detect and delete BOM.
function removeBOM($data) {
if (0 === strpos(bin2hex($data), ‘efbbbf’)) {
return substr($data, 3);
}
return $data;
}
2.Invisible character
Remove invisible characters before parsing.
for ($i = 0; $i <= 31; ++$i) {
$s = str_ replace(chr($i), “”, $s);
}
3.Single quote string value
Let’s look at the following example:
<?php
$s = “{\”x\”:’abcde’}”;
$j = json_ decode($s, true);
var_ dump($j);
echo json_ last_ error() . “\n”;
PHP 5.5. 9 output
NULL
four
Generally, you only need to replace single quotation marks with double quotation marks. During specific processing, you should pay attention to that single quotation marks may also appear in other places. Whether to replace them globally needs to be analyzed according to the specific situation.
Read More:
- [PHP]json_encode Chinese JSON_UNESCAPED_UNICODE returns null in php5.3
- TP5 fuzzy Chinese characters error [How to Solve]
- [Solved] Warning: Unknown: Failed to write session data using user defined save handler. (session.save_path:
- PHP: How to parse MHT file into HTML
- composer Failed to decode response: zlib_decode(): data error
- Unserialize(): cause analysis and solution of error at offset
- [Solved] Artisan error: SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes
- PHP use __Sleep() and __wakeup() method serializes the object
- [PHP] Array to string conversion error when sending data in post
- PHP Error Object of class mysqli could not be converted to string in
- [Solved] error processing package libapache2-mod-php7.2
- 404 solution for WordPress Chinese tag
- Laravel-admin php artisan admin:install error reporting problem solution
- Fatal error: Function name must be a string in
- PHP encryption 3DES error Call to undefined function: mcrypt_module_open() solution
- nginx.conf php-fpm.conf and php.ini Error among the three_ Differences and relations between log instructions
- How to solve Magento 500 internal server errors
- Leetcode 357: How to calculate the number of digits with different digits
- PHP solves the problem that composer exceeds the memory size Allowed memory size
- How to Solve opcache error: zend_mm_heap corrupted