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->字节


    
    
    
    
    
                                        
    
  • 相关阅读:
    VM VirtualBox安装Centos6.5
    桥接
    程序员工作心法
    策略模式-鸭子怎么飞-实例
    策略模式-用什么方式去上班呢 实例
    观察者模式-订报纸,语音呼叫系统实例
    门面(Facade)模式--医院,保安系统实例
    Promise实例的resolve方法
    Promise实例的any方法
    Promise实例的race方法
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349957.html
Copyright © 2011-2022 走看看