在 JSON::XS for Perl 中使用 "u four-hex-digits" 进行编码



我的哈希包含带有 UTF-8 字符的字符串,例如:

$hash = { text => 'Dragón' };

当我使用 JSON::XS 将其编码为 JSON 时,我得到如下所示的内容:

{"text":"Dragón"}

虽然看起来很丑,但有效,但我想得到这样的东西:

{"text":"Dragu00f3n"}

这可能吗?

->ascii会将输出限制为 US-ASCII 字符。

my $json = JSON::XS->new->ascii;
my $text = $json->encode($hash);
use JSON::XS ();
use JSON; # uses JSON::XS by default (if available)
$json_text = to_json( { text => 'Dragón' }, { 'ascii' => 1 } );

最新更新