json_decodeのphpの中のいくつかの解析できない文字列

2593 ワード

jsonについてdecodeのphpでは、以下の一般的なタイプを含む解析できない文字列があります.
一、Bug#42186 json_decode()won'tworkwith\l
文字列にlが含まれている場合、json_decodeは解析できません.テストコード:
echo "***********json_decode() won't work with \l*************
"; $json = '{"stringwithbreak":"line with a \lbreak!"}'; var_dump($json);//stringwithbreak":"line with a \lbreak! var_dump(json_decode($json, true));//null

解決策:
主にlを置き換えるのですが、もちろん本当に'l'が必要なら、jsonを使用しない必要があります.decodeは解析を行い,その文字としてコミットすることができる.
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! ) 

二、TabsinJavascriptstringsbreakjson_decode()
文字列にtabキーが含まれている場合、json_decode()は解析できません.例えば、コード3-1
echo "
***********Tabs in Javascript strings break json_decode()*************
"; var_dump(json_decode('{ "abc": 12, "foo": "bar bar" }'));

実行後の戻り結果null
解決策:
1、tabキー入力を含む文字列に遭遇した場合、jsonを使用してphpにデータを転送し、phpを解析として使用することは避けるべきである.
2、同様に以下の3-2コード方式で置換することができる
$myStr = '{ "abc": 12, "foo": "bar	bar" }';
$replaceStr = str_replace("	", "\\t", $myStr);
var_dump($replaceStr);
var_dump(json_decode($replaceStr ));

三、json_decodereturnsfalsewhenleadingzerosaren'tescapedwithdoublequotes
jsonのvalue値がnumberタイプであり、numberは0で始まる、例えばコード4−1である
echo "
***********json_decode returns false when leading zeros aren't escaped with double quotes*************
"; $noZeroNumber = '{ "test" : 6 }'; $zeroNumber= '{ "test" : 06 }'; var_dump(json_decode($noZeroNumber));//object(stdClass)[1] public 'test' => int 6 var_dump(json_decode($zeroNumber));//null

このような問題はめったに現れないかもしれませんが、いったん現れたら、私たちは問題の原因を探すのが難しいです.
四、d e c o d e c h o kesonunquotedobjectkeys
キー値に引用符が使用されていない場合、コード5-1などの解析はできません.
echo "
***********decode chokes on unquoted object keys*************
"; 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