以编码UTF-8而不是Unicode返回PHP`json_encode`的任何方法吗? [英] Any way to return PHP `json_encode` with encode UTF-8 and not Unicode?

查看:66
本文介绍了以编码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":"á"}

Expected result: {"a":"á"}

推荐答案

{"a":"\u00e1"}{"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天全站免登陆