php字符解析json_decode为null

php字符解析为null

开发遇到json_decode解析的字符串种含有特殊字符解析结果为NULL

直接上例子

$str = '{"wxNum":"ggddss008","wxId":"wxid_txef0o4ekuhi22","nickname":"[emoji=FFFD]i[emoji=FFFD][emoji=FFFD][emoji=FFFD]同庆"}';
var_dump(json_decode($str,true));
//此时运行结果为NUll

解决办法有:
1.字符串转义:mb_convert_encoding()


$str = '{"wxNum":"ggddss008","wxId":"wxid_txef0o4ekuhi22","nickname":"[emoji=FFFD]i[emoji=FFFD][emoji=FFFD][emoji=FFFD]同庆"}';

$strmb= mb_convert_encoding($str,'utf-8','utf-8'); 
var_dump(json_decode($str,true));
//此时运行结果为NUll,也说不定你就成功了

2、正则表达式

$str = '{"wxNum":"ggddss008","wxId":"wxid_txef0o4ekuhi22","nickname":"[emoji=FFFD]i[emoji=FFFD][emoji=FFFD][emoji=FFFD]同庆"}';
$str3 = preg_replace('/[\x00-\x1F\x80-\x9F]/u','',trim($str));
var_dump(json_decode($str3,true));
//运行结果

array(3) {
  ["wxNum"]=>
  string(9) "ggddss008"
  ["wxId"]=>
  string(19) "wxid_txef0o4ekuhi22"
  ["nickname"]=>
  string(55) "[emoji=FFFD]i[emoji=FFFD][emoji=FFFD][emoji=FFFD]同庆"
}

你可能感兴趣的:(php,php,json,开发语言)