問題描述
有沒有辦法用編碼 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`?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!