PHP JSON_ENCODE 不转义中文汉字的方法

2018-09-26 15:14:23

php从数据库查询出来的数据,json_encode以后,保存到本地.json文件


可气的是文件内的编码,基本都是以\u开头的汉子。

这就完蛋了,虽然不影响使用,但是强迫症的我肯定是不满意的。

  1. 是也不一定100%不影响直接使用

  2. 关键是占内存啊。



尝试了php的 吧 \u 等字符串,转换成中文的。试了很多不管用啊。

表示绝望,因为大家都管用。



后来发现不是查询数据库编码的问题。是json_encode的问题。


PHP 生成JSON的时候,必须将汉字不转义为 \u开头的UNICODE数据。

网上很多,但是其实都是错误的,正确的方法是在json_encode 中加入一个参数 JSON_UNESCAPED_UNICODE 

 json_encode($data, JSON_UNESCAPED_UNICODE); //必须PHP5.4+


  • 2017-11-01 01:30:45

    解决第三方包内jar包冲突

    这个问题就是因为引入jar包的冲突,这时我们可以在build.gradle中添加如下代码,下方单独的是添加的代码

  • 2017-11-06 01:00:17

    撤销git add

    如何撤销git add,不小心执行了git add . 操作,但是又不能提交所有的文件,因为对应不同的分支,现在怎么样可以将git add 撤销回来

  • 2017-11-10 00:06:15

    CORS: credentials mode is 'include'

    XMLHttpRequest cannot load http://localhost/Foo.API/token. The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:5000' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.