perl unicode utf-8 \x转换

时间:2021-02-13 20:15:10
[root@dr-mysql01 ~]# cat x5.pl
use Encode;
use JSON; 
use URI::Escape;
use LWP::Simple;
my $host = "http://ip.taobao.com/service/getIpInfo.php?ip=202.101.172.35";
my $content = get($host);
print "\$content  is $content\n";


$content =~ s/\\u([0-9a-fA-F]{4})/pack("U",,hex($1))/eg; 

print "\$content  is $content\n";

use Data::Dumper;

my $json_out= Dumper($content); 
print "\$json_out is $json_out\n"
You have mail in /var/spool/mail/root
[root@dr-mysql01 ~]# perl x5.pl
$content  is {"code":0,"data":{"country":"\u4e2d\u56fd","country_id":"CN","area":"\u534e\u4e1c","area_id":"300000","region":"\u6d59\u6c5f\u7701","region_id":"330000","city":"\u676d\u5dde\u5e02","city_id":"330100","county":"","county_id":"-1","isp":"\u7535\u4fe1","isp_id":"100017","ip":"202.101.172.35"}}
Wide character in print at x5.pl line 12.
$content  is {"code":0,"data":{"country":"中国","country_id":"CN","area":"华东","area_id":"300000","region":"浙江省","region_id":"330000","city":"杭州市","city_id":"330100","county":"","county_id":"-1","isp":"电信","isp_id":"100017","ip":"202.101.172.35"}}
$json_out is $VAR1 = "{\"code\":0,\"data\":{\"country\":\"\x{4e2d}\x{56fd}\",\"country_id\":\"CN\",\"area\":\"\x{534e}\x{4e1c}\",\"area_id\":\"300000\",\"region\":\"\x{6d59}\x{6c5f}\x{7701}\",\"region_id\":\"330000\",\"city\":\"\x{676d}\x{5dde}\x{5e02}\",\"city_id\":\"330100\",\"county\":\"\",\"county_id\":\"-1\",\"isp\":\"\x{7535}\x{4fe1}\",\"isp_id\":\"100017\",\"ip\":\"202.101.172.35\"}}";