PHP 生成JSON问题

WBOY
Release: 2016-06-06 20:32:10
Original
1076 people have browsed it

PHP 生成JSON如何不做unioncode编码转换

比如在json 里面 $a = ['name'=>'张三','age'=>20];在json_encode后 '张三'会变成
'/u97asfddd/' 这样的编码,如何能使'张三'不转换成 '/u/'这样的编码呢?

回复内容:

PHP 生成JSON如何不做unioncode编码转换

比如在json 里面 $a = ['name'=>'张三','age'=>20];在json_encode后 '张三'会变成
'/u97asfddd/' 这样的编码,如何能使'张三'不转换成 '/u/'这样的编码呢?

JSON_UNESCAPED_UNICODE需要php5.4以上的版本才可以使用

<code>PHP</code><code>function json_encode_wrapper ($result)
{
    if(defined('JSON_UNESCAPED_UNICODE')){
        return json_encode($result,JSON_UNESCAPED_UNICODE|JSON_NUMERIC_CHECK);
    }else {
        return preg_replace(
            array("#\\\u([0-9a-f][0-9a-f][0-9a-f][0-9a-f])#ie", "/\"(\d+)\"/",),
            array("iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))", "\\1"),
            json_encode($result)
        );
    }
}
</code>
Copy after login

http://php.net/json_encode

<code>echo json_encode($arr, JSON_UNESCAPED_UNICODE);
</code>
Copy after login
Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!