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";

运行结果:

Perl hash转JSON格式的示例_第1张图片

你可能感兴趣的:(perl,perl,hash,json,数组)