In PHP development, json_decode is a common function for parsing JSON data, but in actual use, you often encounter the problem of "illegal JSON characters" causing parsing failure. This article will give you a comprehensive understanding of how to troubleshoot and resolve illegal JSON characters encountered by json_decode , helping you quickly locate problems and successfully complete JSON parsing.
json_decode is used to convert JSON strings to PHP variables. If the passed string is incorrect, the function will return null , and the specific error code can be obtained through json_last_error() .
Common errors include:
Illegal Character : Unrecognized characters appear in the JSON string.
Coding issues : JSON must be UTF-8 encoding, non-UTF-8 encoding will cause parsing to fail.
Structural error : Missing quotes, commas, bracket mismatch, etc.
When parsing fails, the error code and description are preferred:
<?php
$json = '{"name": "Zhang San", "age": 28,}'; // Note that there is an extra comma at the end
$data = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
echo 'Error code:' . json_last_error() . PHP_EOL;
echo 'error message:' . json_last_error_msg() . PHP_EOL;
}
?>
This code will prompt a "grammar error" and tell you that there is a problem with the JSON structure.
When encountering complex JSON strings, you can copy the string to an online JSON verification tool (such as gitbox.net) to assist in troubleshooting syntax errors.
Non-UTF-8 encoded strings can cause parsing to fail. You can use PHP functions to detect and convert:
<?php
if (!mb_check_encoding($json, 'UTF-8')) {
$json = mb_convert_encoding($json, 'UTF-8', 'auto');
}
?>
Sometimes copying and pasting produces hidden characters (such as BOM) can cause parsing to fail:
<?php
// Remove UTF-8 BOM
$json = preg_replace('/^\x{FEFF}/u', '', $json);
?>
Filter out non-printed characters with regular expressions:
<?php
$json = preg_replace('/[\x00-\x1F\x7F]/u', '', $json);
?>
If unescaped double quotes appear in JSON strings, it will also result in parsing errors:
<?php
$json = str_replace('"', '\"', $json);
?>
Note: This method is only suitable for simple scenarios, and blind replacement is not recommended for complex JSON.
The json_decode parsing that comes with PHP is relatively strict, so you can consider using a more tolerant JSON parsing library, such as:
These libraries can help you better locate and tolerate illegal JSON characters.
Always ensure that the JSON string is formatted correctly, and avoid syntax errors such as unnecessary commas and unclosed quotes.
Confirm that the JSON string is encoded as UTF-8.
Clean up hidden characters and BOM.
Use json_last_error and json_last_error_msg to assist in positioning issues.
For complex or irregular JSON, try to use a special JSON verification tool (gitbox.net recommended) to assist in inspection.
Use third-party libraries to improve the robustness of analysis when necessary.
Master the above investigation skills and when you encounter json_decode parsing failure, you will be at ease, quickly locate and solve the problem!