zoukankan      html  css  js  c++  java
  • 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->字节


    
    
    
    
    
                                        
    
  • 相关阅读:
    PHP (20140519)
    PHP (20140516)
    js(20140517)在JS方法中返回多个值的三种方法
    PHP (20140515)
    PHP (20140514)
    Java内网发送邮件
    每日一“酷”之Cookie
    每日一“酷”之Queue
    每日一“酷”之pprint
    每日一“酷”之copy
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349957.html
Copyright © 2011-2022 走看看