perl $d = encode_utf8($r); $f = decode_json($d)

[root@dr-mysql01 ~]# cat a1.pl
my $url="http://192.168.32.101:3000/api/getcode?env=zj&phone=180727xx237";
use  LWP::UserAgent;
use Encode;
 
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
$ua->agent("Mozilla/8.0");

my $response = $ua->get($url);



if ($response->is_success) {
 $r= $response->decoded_content;  # or whatever
}
else {
 die $response->status_line;
};
 use JSON qw/encode_json decode_json/;
 $d       = encode_utf8($r);
 $f       = decode_json($d);
print $f;
print "\n";
use Data::Dumper;
my $var= Dumper($f);
print $var;
print "\n";
print $f->[0]->[1];
[root@dr-mysql01 ~]# perl a1.pl
ARRAY(0x1315750)
$VAR1 = [
          [
            '18072722237',
            "ZJ\x{e6}\x{b2}\x{a1}\x{e6}\x{9c}\x{89}\x{e6}\x{89}\x{be}\x{e5}\x{88}\x{b0}\x{e9}\x{9c}\x{80}\x{e8}\x{a6}\x{81}\x{e7}\x{9a}\x{84}\x{e9}\x{aa}\x{8c}\x{e8}\x{af}\x{81}\x{e7}\x{a0}\x{81}\x{e4}\x{bf}\x{a1}\x{e6}\x{81}\x{af}"
          ]
        ];

ZJ没有找到需要的验证码信息[root@dr-mysql01 ~]# 



[root@dr-mysql01 ~]# cat a2.pl 
my $url="http://192.168.32.101:3000/api/getcode?env=zj&phone=180727xx237";
use  LWP::UserAgent;
use Encode;
 
my $ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
$ua->agent("Mozilla/8.0");

my $response = $ua->get($url);



if ($response->is_success) {
 $r= $response->decoded_content;  # or whatever
}
else {
 die $response->status_line;
};
 use JSON qw/encode_json decode_json/;
 #$d       = encode_utf8($r);
 $f       = decode_json($r);
print $f;
print "\n";
use Data::Dumper;
my $var= Dumper($f);
print $var;
print "\n";
print $f->[0]->[1];
[root@dr-mysql01 ~]# perl a2.pl 
ARRAY(0x1f3e878)
$VAR1 = [
          [
            '18072722237',
            "ZJ\x{6ca1}\x{6709}\x{627e}\x{5230}\x{9700}\x{8981}\x{7684}\x{9a8c}\x{8bc1}\x{7801}\x{4fe1}\x{606f}"
          ]
        ];

Wide character in print at a2.pl line 29.
ZJ没有找到需要的验证码信息[root@dr-mysql01 ~]# 

在数据中含有UTF-8字符的时候需要稍微注意,如果直接按照上面的方法将会出现乱码。JSON模块的encode_json和decode_json自身是支持UTF8编码的,但是perl为了简洁高效,默认是认为程序是非UTF8的,因此在程序开头处需要申明需要UTF8支持

posted @ 2016-05-20 10:11  czcb  阅读(239)  评论(0编辑  收藏  举报