有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? [英] Any way to return PHP `json_encode` with encode UTF-8 and not Unicode?

查看:23
本文介绍了有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法用编码 UTF-8 而不是 Unicode 返回 PHP json_encode?

Any way to return PHP json_encode with encode UTF-8 and not Unicode?

$arr=array('a'=>'á');
echo json_encode($arr);

mb_internal_encoding('UTF-8');$arr=array_map('utf8_encode',$arr); 没有修复它.

结果:{"a":"u00e1"}

预期结果:{"a":"á"}

推荐答案

{"a":"u00e1"} and {"a":"á"} 是编写相同 JSON 文档的不同方式;JSON 解码器将解码 unicode 转义.

{"a":"u00e1"} and {"a":"á"} are different ways to write the same JSON document; The JSON decoder will decode the unicode escape.

在 php 5.4+ 中,php 的 json_encode 确实有用于纯输出的 JSON_UNESCAPED_UNICODE 选项.在较旧的 php 版本上,您可以推出自己的不编码非 ASCII 字符的 JSON 编码器,或使用 Pear 的 JSON 编码器并删除第 349 到 433 行.

In php 5.4+, php's json_encode does have the JSON_UNESCAPED_UNICODE option for plain output. On older php versions, you can roll out your own JSON encoder that does not encode non-ASCII characters, or use Pear's JSON encoder and remove line 349 to 433.

这篇关于有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆