JSON在Perl中编码/解码utf8字符串

时间:2016-02-18 23:26:57

标签: json perl utf-8

我将命令行中的utf8编码字符串传递给Perl程序:

> ./test.pl --string='ḷet ūs try ṭhiñgs'

似乎正确识别了字符串:

use utf8;  

GetOptions(                                                                     
    'string=s' => \$string,                                                     
) or die;                                                                    
print Dumper($string);
print Dumper(utf8::is_utf8($string));                                           
print Dumper(utf8::valid($string));                                             

打印

$VAR1 = 'ḷet ūs try ṭhiñgs';
$VAR1 = '';
$VAR1 = 1;

当我将此字符串存储到哈希并在其上调用encode_json时,字符串似乎再次编码,而to_json似乎有效(如果我读取输出正确地):

my %a = ( 'nāme' => $string ); # Note the Unicode character                                                 
print Dumper(\%a);
print Dumper(encode_json(\%a));                                                 
print Dumper(to_json(\%a));                                                     

打印

$VAR1 = {
          "n\x{101}me" => 'ḷet ūs try ṭhiñgs'
        };
$VAR1 = '{"nāme":"ḷet Å«s try á¹­hiñgs"}';
$VAR1 = "{\"n\x{101}me\":\"\x{e1}\x{b8}\x{b7}et \x{c5}\x{ab}s try \x{e1}\x{b9}\x{ad}hi\x{c3}\x{b1}gs\"}";

然而,将其转换回原始哈希值似乎不适用于任何一种方法,并且在两种情况下都是哈希,字符串和破坏:

print Dumper(decode_json(encode_json(\%a)));                                    
print Dumper(from_json(to_json(\%a)));    

打印

$VAR1 = {
           "n\x{101}me" => "\x{e1}\x{b8}\x{b7}et \x{c5}\x{ab}s try \x{e1}\x{b9}\x{ad}hi\x{c3}\x{b1}gs"
        };
$VAR1 = {
          "n\x{101}me" => "\x{e1}\x{b8}\x{b7}et \x{c5}\x{ab}s try \x{e1}\x{b9}\x{ad}hi\x{c3}\x{b1}gs"
        };

哈希查找$a{'nāme'}现在失败。

问题:如何在Perl中正确处理utf8编码和字符串以及JSON编码/解码?

1 个答案:

答案 0 :(得分:2)

您需要解码输入:

use Encode;

my $string;
GetOptions('string=s' => \$string) or die;
$string = decode('UTF-8', $string);

总而言之,我们得到:

use strict;
use warnings;
use 5.012;
use utf8;

use Encode;
use Getopt::Long;
use JSON;

my $string;
GetOptions('string=s' => \$string) or die;
$string = decode('UTF-8', $string);

my %hash = ('nāme' => $string);
my $json = encode_json(\%hash);
my $href = decode_json($json);

binmode(STDOUT, ':encoding(utf8)');
say $href->{nāme};

示例:

$ perl test.pl --string='ḷet ūs try ṭhiñgs'
ḷet ūs try ṭhiñgs

确保您的源文件实际编码为UTF-8!