使用Perl编码由Java程序使用的哈希.JSON编码将单个键转换为JSON数组元素.
my %attribute;
push (@{$attribute {"Color"}},'Green');
push (@{$attribute {"Model"}}, ('Model_1','Model_2'));
print Dumper(\%attribute);
my $json_attr = JSON->new->utf8->encode(\%attribute);
print $json_attr;
Run Code Online (Sandbox Code Playgroud)
输出:
$VAR1 = {
'Model' => [
'Model_1',
'Model_2'
],
'Color' => [
'Green'
]
};
{"Model":["Model_1","Model_2"],"Color":["Green"]}
Run Code Online (Sandbox Code Playgroud)
我需要哈希中的单个键看起来像这样:{"Color":"Green"}(没有方括号)谢谢.