JSON_decode 解码返回 null 的问题

需求确定:当遇到数据进行json_decode($out,true);解码时返回值为null
问题排除:
1、确定你的字符串是否为正确的json格式
2、在json_decode后用json_last_error()查看返回的错误信息;
返回错误类型有以下几种情况:
int(0) : JSON_ERROE_NONE No error has occured
int(1): JSON_ERROR_DEPTH the maximum stack depth has been exceeded
int(2): JSON_ERROR_STATE_MISMATCH Invalid or malformed JSON 无效或不正确的json形式
int(3): JSON_ERROR_CTRL_CHAR control character error,possibly incorrectly encode
int(4): JSON_ERROR_SYNTAX syntax error 语法错误
int(5): JSON_ERROR_UTF8 Malformed UTF-8 characters,possibly incorrectly...

解决方案:
1、当返回结果为5的时候,是因为编码问题导致,所以通过转码就可以解决问题,如下所示(iconv转码有时会出错,所以个人建议用下面的函数进行转码)
$output = mb_convert_encoding($output,'utf-8',"auto");

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!