2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > php json_encode后乱码 PHP中json_encode后中文乱码的解决方案

php json_encode后乱码 PHP中json_encode后中文乱码的解决方案

时间:2021-04-22 00:37:55

相关推荐

php json_encode后乱码 PHP中json_encode后中文乱码的解决方案

header("Content-Type:text/html;charset=utf-8;");

$arr = array (Version_code=>2,Version_name=>UpdateVersion,Versoin_desc=>更新了地图功能,Versoin_path=>/BusIot/APK/BusIot.apk);

echo json_encode($arr);

?>

如上代码,即为将字符串变量转化成json格式输出,但是输出结果如下:

{"Version_code":2,"Version_name":"UpdateVersion","Versoin_desc":"\u66f4\u65b0\u4e86\u5730\u56fe\u529f\u80fd","Versoin_path":"http:\/\/\/BusIot\/APK\/BusIot.apk"}

即数组中所有中文在json_encode之后都不见了或者出现\u2353等。

解决方法是用urlencode()函数处理以下,在json_encode之前,把所有数组内所有内容都用urlencode()处理一下,然用json_encode()转换成json字符串,最后再用urldecode()将编码过的中文转回来。

header("Content-Type:text/html;charset=utf-8;");

$arr = array (

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。