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?

