perl $d = encode_utf8($r); $f = decode_json($d)
2016-05-20 10:11
453 查看
[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支持
相关文章推荐
- perl $d = encode_utf8($r); $f = decode_json($d)
- perl $d = encode_utf8($r); $f = decode_json($d)
- jsp下拉选框赋值(在js里进行)
- JavaScript RegExp 对象
- javascript笔记---算法基础学习
- C# litJson 使用方法
- 使用百度地图API在页面添加百度地图应用
- jsp iframe example
- javascript的理解及经典案例分析
- javascript的replace+正则 实现ES6的字符串模版
- 12个JavaScript技巧
- javascript中的swith用法
- [转]TextArea设置MaxLength属性最大输入值的js代码
- JS中对象与字符串的互相转换详解
- MVC5中后台提供Json,前台处理Json,绑定给Dropdownlist的例子
- js各种间距数据汇总
- 浅析JavaScript Array和string的转换(推荐)
- [Effective JavaScript 笔记] 第12条:理解变量声明提升
- 深入理解JS addLoadEvent函数
- 推荐10 个短小却超实用的 JavaScript 代码段