Some unresolvable strings in PHP of json_decode include the following common types.
I. Bug #42186 json_decode () won't work with \ L
When the string contains \ L, json_decode cannot be parsed. Test code:
echo "***********json_decode() won't work with \l*************<br/>";$json = '{"stringwithbreak":"line with a \lbreak!"}';var_dump($json);//stringwithbreak":"line with a \lbreak!var_dump(json_decode($json, true));//null
Solution:
It is mainly to replace \ L. Of course, if '\ l' is really needed, we must not use json_decode for parsing and commit as a character.
var_dump(str_replace("\\l", "", $json));//stringwithbreak":"line with a break!print_r(json_decode(str_replace("\\l", "", $json), true));//Array ( [stringwithbreak] => line with a break! )
Ii. tabs in Javascript Strings Break json_decode ()
When the string contains the tab key, json_decode () cannot be parsed, for example, code 3-1.
echo "<br/>***********Tabs in Javascript strings break json_decode()*************<br/>";var_dump(json_decode('{ "abc": 12, "foo": "barbar" }'));
The returned result after execution is null.
Solution:
1. When encountering a string containing the tab key input, we should avoid uploading data to PhP using JSON, and then use PHP for parsing.
2. You can also use the following 3-2 code to replace
$myStr = '{ "abc": 12, "foo": "barbar" }';$replaceStr = str_replace("", "\\t", $myStr);var_dump($replaceStr);var_dump(json_decode($replaceStr ));
3. json_decode returns false when leading zeros aren't escaped with double quotes
If the value of JSON is of the number type and the number starts with 0, for example, code 4-1
echo "<br/>***********json_decode returns false when leading zeros aren't escaped with double quotes*************<br/>";$noZeroNumber = '{ "test" : 6}';$zeroNumber= '{ "test" : 06}';var_dump(json_decode($noZeroNumber));//object(stdClass)[1] public 'test' => int 6var_dump(json_decode($zeroNumber));//null
This problem may rarely occur, but once it occurs, it is difficult to find the cause of the problem.
4. Decode chokes on unquoted object keys
If the key value is not enclosed in quotation marks, it cannot be parsed, for example, Code 5-1.
echo "<br/>***********decode chokes on unquoted object keys*************<br/>";var_dump(json_decode('{"a":"tan","model":"sedan"}'));//object(stdClass)[1] public 'a' => string 'tan' (length=3) public 'model' => string 'sedan' (length=5)var_dump(json_decode('{a:"tan","model":"sedan"}'));//null