php5.4以上:
json_encode($data, JSON_FORCE_OBJECT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
php5.3
//数组转成对象编码,encode后[]变成{}
$data = json_encode($data, JSON_FORCE_OBJECT);
//不转义斜杠
$data = str_replace("\\/", "/", $data);
//以字面编码多字节 Unicode 字符(默认是编码成 \uXXXX)
$data = _unicodeToUTF8($data);
//\u变成多字节Unicode字符
function _unicodeToUTF8($str){
return preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
create_function(
'$matches',
'return mb_convert_encoding(pack("H*", $matches[1]), "UTF-8", "UCS-2BE");'
), $str);
}
注意:JSON_FORCE_OBJECT //慎用,会把里面所有数组都转换成对象,即[]都会变成{}