Perl hash转JSON格式的示例

#encode the JSON result into UTF-8,
$json = JSON->new->utf8;

#验证 utf8 标志位
print "utf8 flag: ",$json->get_utf8,"\n";

#输入是hash
my $hash = {1=>111, 2=>222,3=>333};
print "scalar var: ", $json->encode($hash),"\n";

#输入是hash数组,传入的是数组引用
my @hash = ();
push @hash ,{1=>111};
push @hash ,{1=>222};
print "array ref: ", $json->encode(\@hash),"\n";

#输入是匿名数组
print "anonymous array: ",$json->encode([{name=>"array"},{1=>222}]),"\n";

#输入是匿名hash
print "anonymous hash: ",$json->encode({name=>"hash"}),"\n";

运行结果:

这里写图片描述

posted @ 2018-01-07 10:26  小怪兽&奥特曼  阅读(369)  评论(0编辑  收藏  举报