index.php
$str = include("data.php"); //放一个gbk编码的文件,返回json字符串
// 替换非法utf8字符串为“?”
$str = preg_replace('/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]'.
'|[\x00-\x7F][\x80-\xBF]+'.
'|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*'.
'|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})'.
'|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/S',
'?', $str );
$str = preg_replace('/\xE0[\x80-\x9F][\x80-\xBF]'.
'|\xED[\xA0-\xBF][\x80-\xBF]/S','?', $str );
// 解析
$arr = json_decode($str,true);
echo "<pre>";
if(!$arr){
print_r(json_last_error()); //错误码
}else{
print_r($arr);
}
错误码对照表
json_last_error错误msg对照表:
0 = JSON_ERROR_NONE
1 = JSON_ERROR_DEPTH
2 = JSON_ERROR_STATE_MISMATCH
3 = JSON_ERROR_CTRL_CHAR
4 = JSON_ERROR_SYNTAX
5 = JSON_ERROR_UTF8
data.php 设置为gbk编码
<?php
return '{"messageType" : "Test萨达", "from" : "F2D0B5C6-9875-46B5-8D4F"}';