zoukankan      html  css  js  c++  java
  • PHP中json_encode后,在json字符串中依然显示中文的解决方案

    1  <?php
    2  header("Content-Type:text/html;charset=utf-8;");
    3  $arr = array ('Version_code'=>2,'Version_name'=>'UpdateVersion','Versoin_desc'=>'更新了地图功能','Versoin_path'=>'http://nnddkj.com/BusIot/APK/BusIot.apk');
    4  echo json_encode($arr);
    5  ?>

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

    {"Version_code":2,"Version_name":"UpdateVersion","Versoin_desc":"u66f4u65b0u4e86u5730u56feu529fu80fd","Versoin_path":"http://nnddkj.com/BusIot/APK/BusIot.apk"}

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

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

    1 <?php
    2 header("Content-Type:text/html;charset=utf-8;");
    3 $arr = array ('Version_code'=>2,'Version_name'=>'UpdateVersion','Versoin_desc'=>urlencode('更新了地图功能'),'Versoin_path'=>urlencode('http://nnddkj.com/BusIot/APK/BusIot.apk'));
    4 echo urldecode(json_encode($arr));
    5 ?>

    输出结果:{"Version_code":2,"Version_name":"UpdateVersion","Versoin_desc":"更新了地图功能","Versoin_path":"http://nnddkj.com/BusIot/APK/BusIot.apk"}

    搞定。。

    附:json_decode对JSON格式的字符串进行编码,而json_encode对变量进行 JSON 编码

    json_decode - 对JSON 格式的字符串进行编码

    说明:
    mixed json_decode ( string $json [, bool $assoc ] )
    接受一个 JSON 格式的字符串并且把它转换为 PHP 变量 ,$assoc,当该参数为 TRUE 时,将返回 array 而非 object

    json_encode:详细问度娘

  • 相关阅读:
    Ink——一款使用React风格开发命令行界面应用(CLI App)的nodejs工具
    编程语言相关名词解释汇总
    使用Webpack对Css文件压缩处理的思考
    一种通过async/await实现函数同步执行的方式
    成长的道路上,我很幸运
    Todolist分别用React与Vue的实现与思考
    Unity 框架篇
    扇形技能指示器
    C# Socket和protoBuf新手村教程
    判断点在多边形内部
  • 原文地址:https://www.cnblogs.com/Steven-shi/p/5196823.html
Copyright © 2011-2022 走看看