Home > Backend Development > PHP Tutorial > PHP中json_encode让Json更懂中文(JSON_UNESCAPED_UNICODE)

PHP中json_encode让Json更懂中文(JSON_UNESCAPED_UNICODE)

WBOY
Release: 2016-06-20 13:01:47
Original
1098 people have browsed it

我们知道, 用PHP的json_encode来处理中文的时候, 中文都会被编码, 变成不可读的, 类似”\u***”的格式, 还会在一定程度上增加传输的数据量. 

echo json_encode("中文"); //Output: "\u4e2d\u6587"
Copy after login

这就让我们这些在天朝做开发的同学, 很是头疼, 有的时候还不得不自己写json_encode.而在PHP5.4, 这个问题终于得以解决, Json新增了一个选项: JSON_UNESCAPED_UNICODE, 故名思议, 就是说, Json不要编码Unicode.

看下面的例子:

echo json_encode("中文", JSON_UNESCAPED_UNICODE); //Output: "中文"
Copy after login

怎么样, 是不是让大家很开心的改动?

 当然, Json在5.4还加入了: JSON_BIGINT_AS_STRING, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES等选项

如果有兴趣, 大家可以参看: json_encode

 


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template