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

来源:互联网 发布:电脑打电话软件 编辑:程序博客网 时间:2024/06/05 05:06
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 "\n"; print decode_utf8($json_out);print "\n";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 "\n"; print decode_json($json_out)->[0]->{name};print "\n";centos6.5:/root#perl t1.pl [{"name":"科比","age":19},{"age":25,"name":"乔丹"}]科比字符<-decode_json<-字节  字符->encode_json->字节


                                             
0 0
原创粉丝点击