Perl hash转JSON格式的示例

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

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

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

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

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

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

运行结果:

这里写图片描述

原文地址:https://www.cnblogs.com/jinxiang1224/p/8468172.html