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

当PHP的json_encode处理中文字符时可能出现乱码,解决方案是在json_encode前使用urlencode编码,然后json_encode后再用urldecode解码。通过此方法可以确保中文在json中的正确显示。
摘要由CSDN通过智能技术生成

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

$arr = array ('Version_code'=>2,'Version_name'=>'UpdateVersion','Versoin_desc'=>'更新了地图功能','Versoin_path'=>'http://nnddkj.com/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:\/\/nnddkj.com\/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 ('

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值