encode_json 转换给定的perl数据结构为一个UTF-8编码的 2进制字符串 decode_json把UTF-8字节转换成字符


centos6.5:/root#perl t1.pl 
[{"age":19,"name":"ç§æ¯"},{"name":"ä¹ä¸¹","age":25}]
[{"age":19,"name":"科比"},{"name":"乔丹","age":25}]
centos6.5:/root#cat t1.pl 
use JSON qw/encode_json decode_json/;      
use Encode;  
my $data = [      
    {    
        'name' => '科比',    
        'age' => 19    
    },    
    {    
        'name' => '乔丹',    
        'age' => 25    
    }    
];    
my $json_out = encode_json($data);      
print $json_out;    
print "
"; 

print decode_utf8($json_out);
print "
";
centos6.5:/root#perl t1.pl 
[{"name":"ç§æ¯","age":19},{"name":"ä¹ä¸¹","age":25}]
[{"name":"科比","age":19},{"name":"乔丹","age":25}]

$json_text = encode_json $perl_scalar  
Converts the given Perl data structure to a UTF-8 encoded, binary string.  




$perl_scalar = decode_json $json_text    
The opposite of encode_json: expects an UTF-8 (binary) string and tries to parse that as an UTF-8 encoded JSON text, returning the resulting referenc.  

decode_json 自动解Utf8 字节

centos6.5:/root#cat t1.pl 
use JSON qw/encode_json decode_json/;      
use Encode;  
my $data = [      
    {    
        'name' => '科比',    
        'age' => 19    
    },    
    {    
        'name' => '乔丹',    
        'age' => 25    
    }    
];    
my $json_out = encode_json($data);      
print $json_out;    
print "
"; 


print decode_json($json_out)->[0]->{name};
print "
";
centos6.5:/root#perl t1.pl 
[{"name":"ç§æ¯","age":19},{"age":25,"name":"ä¹ä¸¹"}]
科比


字符<-decode_json<-字节  
字符->encode_json->字节