json_decode为空问题


在error_log日志中打出 json_last_error() 和 json_last_error_msg()

发现 4 和 'Syntax error'


可以用base64_encode记录下要解的$response数据  然后在本地base64_decode出来 再进行json_decode  查为什么不能解开


测试了

$json = iconv('GBK','utf-8',$json);

$json = stripslashes($json);

$json = htmlspecialchars_decode($json);

等多种方法 都不行


最后发现返回的response数据有bom头字符


$response = trim($response,chr(239).chr(187).chr(191));
$response = json_decode($response, true);


这样正常解开了


base64_decode出来的json数据有没有bom头字符 除了网上说的其它方法  也可以放到json在线编辑器中查看




附:

json_decode要求的字符串比较严格:
(1)使用UTF-8编码
(2)不能在最后元素有逗号
(3)不能使用单引号
(4)不能有\r,\t,如果有请替换

你可能感兴趣的:(php)