perl 公交车查询

来源:互联网 发布:网络打印是什么意思 编辑:程序博客网 时间:2024/04/20 19:39
<pre name="code" class="cpp">decode_json 必须是unicode形式的字符,Dump不支持显示unicode形式的中文 只能 \x{xxxx}use URI::Escape;use Encode;use JSON qw/encode_json decode_json/;my $str="杭州";my $str=uri_escape("$str"); print "\$str is $str\n";use LWP::UserAgent;my  $ua = LWP::UserAgent->new;my  @header = (    'accept'=> "application/json",    'content-type'=> "application/json",    'apikey'=> "3074a7f9926a4bbb484aa8bb366e5b87",    'User-Agent' => "Mozilla/8.0"    );my $url = "http://apis.baidu.com/xiaota/bus_lines/buses_lines?city=$str&bus=$ARGV[0]&direction=$ARGV[1]";$request = HTTP::Request->new(GET=>"$url");$request->header(@header);$response = $ua->request($request); $content= $response->decoded_content;$content =~ s/\\u([0-9a-fA-F]{4})/pack("U",,hex($1))/eg; $content  = encode_utf8($content);my $json_out = $content ; print $json_out;##解码json数据my $json_out = decode_json($content);print "111111111111\n";use Data::Dumper;#my $json_out= Dumper($json_out->{data}->{stations}); #print $json_out;#print "\n";foreach (@{($json_out->{data}->{stations})}){print encode_utf8($_->{stateName})."\n";};[root@dr-mysql01 ~]# perl x2.pl 307 1$str is %E6%9D%AD%E5%B7%9E{"code":1000,"msg":"success!","data":{"buses":[{"busId":0,"station":10,"state":0,"distance":246,"reporTime":1}],"stations":[{"station":1,"stateName":"黄龙体育中心"},{"station":2,"stateName":"教工路花园亭"},{"station":3,"stateName":"花园新村"},{"station":4,"stateName":"浙江工商大学"},{"station":5,"stateName":"保亭巷"},{"station":6,"stateName":"教工路塘河路口"},{"station":7,"stateName":"大关桥西"},{"station":8,"stateName":"长乐路"},{"station":9,"stateName":"二纺机总厂"},{"station":10,"stateName":"沈塘湾"},{"station":11,"stateName":"善贤社区"},{"station":12,"stateName":"善贤坝"},{"station":13,"stateName":"皋亭坝"},{"station":14,"stateName":"李王桥"},{"station":15,"stateName":"袁家村"},{"station":16,"stateName":"张家园"},{"station":17,"stateName":"杭玻"},{"station":18,"stateName":"焦化分厂"},{"station":19,"stateName":"沈家桥"},{"station":20,"stateName":"毛竹山"},{"station":21,"stateName":"薄板分厂"},{"station":22,"stateName":"刘文村"}]}}111111111111黄龙体育中心教工路花园亭花园新村浙江工商大学保亭巷教工路塘河路口大关桥西长乐路二纺机总厂沈塘湾善贤社区善贤坝皋亭坝李王桥袁家村张家园杭玻焦化分厂沈家桥毛竹山薄板分厂刘文村


                                             
0 0
原创粉丝点击