1. Error description
For example, the following valid JSON string:
1 | {"code":200,"name":"This is a Chinese character"} |
Use the following code to parse:
1 | $data = json_decode(mb_convert_encoding($txt, 'UTF-8'), true); |
Error JSON invalid.
2. Solution
The problem maybe is that there are invisible characters, you can try to remove:
1 | $data = json_decode(preg_replace('/[\x00-\x1F\x80-\xFF]/', '', mb_convert_encoding($txt, 'UTF-8')), true); |
According to the test, the problem can be solved, if it still can't be solved, you can try to check whether the slash/backslash is grammatical.
Fix JSON Decode Error Caused by Special Characters in PHP
Comments