【问题标题】:How to handle unicode in \uXXXX format? [closed]如何处理 \uXXXX 格式的 unicode? [关闭]
【发布时间】:2016-08-08 03:36:34
【问题描述】:

如果有文字:

“专业纪律委员会” 魁北克农业"

我该如何处理\u0027,这显然是某种撇号。如何将其转换为可读字符?

数据来自JSON

【问题讨论】:

标签: json perl unicode


【解决方案1】:
use utf8;
use JSON::XS;
use open OUT => ':utf8';
my $decoder = JSON::XS->new->allow_nonref(1);
my $json_string = '"Conseil de discipline de l\\u0027Ordre des comptables professionnels agréés du Québec"';
printf "in: %s\nout: %s\n", $json_string, $decoder->decode($json_string);

为我工作。 (使用 perl 源中文字 utf8 所需的 utf8,allow_nonref 只需要解码一个字符串,而不是对象或数组)

如果你确实有一些双重编码的字符串,你可以这样做:

$string =~ s/\\u([[:xdigit:]]{4})/chr hex $1/g;

【讨论】:

  • 我在decode_json 行收到Wide character in subroutine entry 错误。
  • 看来use open OUT => ':utf8'; 是不必要的。我说的对吗?
猜你喜欢
  • 2013-10-17
  • 1970-01-01
  • 2021-03-01
  • 2012-07-28
  • 1970-01-01
  • 2011-04-06
  • 2014-01-17
  • 2011-04-19
  • 1970-01-01
相关资源
最近更新 更多