zoukankan      html  css  js  c++  java
  • perl json模块

    JSON - JSON (JavaScript Object Notation) encoder/decoder  
    
    简介:
    
     use JSON; # imports encode_json, decode_json, to_json and from_json.
     
     # simple and fast interfaces (expect/generate UTF-8)
     
     $utf8_encoded_json_text = encode_json $perl_hash_or_arrayref;
     $perl_hash_or_arrayref  = decode_json $utf8_encoded_json_text;
     
     # OO-interface
     
     $json = JSON->new->allow_nonref;
     
     $json_text   = $json->encode( $perl_scalar );
     $perl_scalar = $json->decode( $json_text );
     
     $pretty_printed = $json->pretty->encode( $perl_scalar ); # pretty-printing
     
     # If you want to use PP only support features, call with '-support_by_pp'
     # When XS unsupported feature is enable, using PP (de|en)code instead of XS ones.
     
     use JSON -support_by_pp;
     
     # option-acceptable interfaces (expect/generate UNICODE by default)
     
     $json_text   = to_json( $perl_scalar, { ascii => 1, pretty => 1 } );
     $perl_scalar = from_json( $json_text, { utf8  => 1 } );
     
     # Between (en|de)code_json and (to|from)_json, if you want to write
     # a code which communicates to an outer world (encoded in UTF-8),
     # recommend to use (en|de)code_json.
     
     
     这个版本 是兼容JSON::XS 2.34 和以后版本
     
     注意:
     
     JSON::PP 是很早包含在JSON发布版本, 在Perl 5.14 变成一个核心模块。
     
     
     对于这个会话,JSON::PP 是从JSON 发布版本中移除, 现在可以在Perl5的库中发现
     
     JSON 是一个简单的数据类型:
     
     
     这个模块转换Perl的数据结构到JSON 
     
     JSON::XS  是最快的和最适当的JSON模块在CPAN ,必须被编译和安装在你的环境
     
     JSON::PP 是一个纯-Perl的模块 
     
     区分 模块名'JSON'和格式JSON, 前者是引号通过C<>(它的结果变化和你使用的介质),
     
     
     Module name : JSON
    
    Format type : JSON
    
    FEATURES
    
    1.正确处理unicode
    
    
    2. 这个模块(即后端模块) 知道如何处理Unicode,如何和何时做, 甚至记录正确意味着什么
    
    
    尽管有局限性,这个功能是可用的在Perl 5.6以后
    
    
    JSON::XS 需要Perl 5.8.2(但是正确工作在5.8.8或者以后版本),
    
    因此在老的版本JSON 应该调用JSON::PP 作为后台 
    
    3. 往返的完整性:
    
    当你持续一个perl 数据结果只使用被JSON和PERL支持的数据类型,
    
    
    描述的数据类型结构是相同的在Perl层面。
    
    JSON 的严格检查的正确性
    
    
    没有猜测,没有产生非法的JSON 文本, 只有JSON 是被接收作为输入
    
    
    fast
    
    模块返回一个 JSON::XS 对象本身。相比其他的JSON模块和其他的serialisers 比如存储,
    
    
    simple to use  使用简单
    
    该模块即有一个简单的函数接口以及一个面向对象的接口
    
    
    合理通用输出格式
    
    
    
    你可以选择在最紧凑的单行格式可能的选择(很好的对于简单行基于协议),
    
    一个纯ASCII 格式(对于当你的传输不是9位的,仍旧支持整个Unicode 范围0
    
    
    函数接口:
    
    一些文件被辅助,从函数接口修改
    
    encode_json
    
        $json_text = encode_json $perl_scalar
    	
    	转换给定的perl 数据结构到一个UTF-8编码的,2进制字符串
    	
    	
    	decode_json
    
        $perl_scalar = decode_json $json_text
    
    
    	
    	和encode_json 相反,期望是一个UTF-8(binary) 字符串和尝试解析一个UTF-8编码的JSON文本,
    	
    	返回一个结果引用
    	
    	
    	如何从外部解码数据和编码数据到外部
    	
    	这个章节假设你的Perl版本是5.8或者更高
    	
    	
    	如果你知道一个JSON文本从外部世界 -a 网络,一个文件格式等等,
    	
    	是被编码成UTF-8,,你需要使用decode_json 或者JSON模块对象在启用UTF8的情况下。
    	
    	
    	解码结果会包含UNICODE 字符串
    	
    	  # from network
      my $json        = JSON->new->utf8;
      my $json_text   = CGI->new->param( 'json_data' );
      my $perl_scalar = $json->decode( $json_text );
      
      # from file content
      local $/;
      open( my $fh, '<', 'json.data' );
      $json_text   = <$fh>;
      $perl_scalar = decode_json( $json_text );
    
    
      如果外部数据不是UTF-8编码的,首先你必须decode 它的结果变化和你使用的介质
       use Encode;
      local $/;
      open( my $fh, '<', 'json.data' );
      my $encoding = 'cp932';
      my $unicode_json_text = decode( $encoding, <$fh> ); # UNICODE
      
      # or you can write the below code.
      #
      # open( my $fh, "<:encoding($encoding)", 'json.data' );
      # $unicode_json_text = <$fh>;
      
      
      在这种情况下, $unicode_json_text 当然是UNICODE string.
      
      
      因此你不需要使用 decode_json nor JSON module object 启用utf8.
      
      
      现在, 你需要转换你的$perl_scalar 为JSON 数据和发送它到一个外部世界 -a 网络 或者文件内容
      
      
      你的数据通常包含UNICODE 字符串和你需要转换数据为UTF-8编码的,
      
      如果 $perl_scalar不包含UNICODE 但是$encoding-encoded字符串由于某些原因,
      
      然后它的字符串是被对待为latin1 在perl里(因为它不关心你的$encoding). 
      
      
      你不能使用 encode_json nor JSON 模块对象在UTF8启用的情况下。

  • 相关阅读:
    alpha冲刺9
    alpha冲刺8
    alpha冲刺7
    alpha冲6
    随堂小测-同学录
    alpha冲刺5
    任务3
    任务2
    任务1
    网站用户行为分析
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13350548.html
Copyright © 2011-2022 走看看