You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP中json_encode默认将UTF-8字符编码为十六进制实体的原因是什么?

Hey folks, let's break down how PHP's json_encode() handles UTF-8 characters by default, plus how to adjust this behavior if you need to.

By default, when you pass a UTF-8 string to json_encode() in PHP, it will convert the characters into hexadecimal Unicode entities. Here's a concrete example:

$c = json_encode("涓"); // 返回 "\u4e2d"

If you want to output the raw UTF-8 character instead of the escaped entity, you just need to add the JSON_UNESCAPED_UNICODE flag as the second parameter to the function:

$c = json_encode("涓", JSON_UNESCAPED_UNICODE); // 返回 "涓"

As for why this is the default behavior? It’s all about backward compatibility. Older JSON parsers didn’t have robust support for UTF-8, so converting characters to these hex entities ensured the JSON would be readable across a wider range of legacy systems. These days, though, most modern parsers handle UTF-8 flawlessly, so using JSON_UNESCAPED_UNICODE is totally safe—and often preferred for more human-readable output.


内容的提问来源于stack exchange,提问作者 JSON_UNESCAPED_UNICODE); // return ""涓�"

Why this default?

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:35:35