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:详细问度娘

  • 相关阅读:
    Jmeter之断言处理
    JMeter工具简单介绍
    TCP的三次握手和四次挥手
    浅谈cookie、session
    浅谈HTTP中Get与Post的区别
    DNS原理入门
    互联网协议简介
    测试与部署
    部分代码片段
    apache和php扩展问题
  • 原文地址:https://www.cnblogs.com/Steven-shi/p/5196823.html
Copyright © 2011-2022 走看看