我有一个这样的文件:
{
"dev":
{
"data": "1"
},
"stg":
{
"data": "1"
},
"pre":
{
"data": "1"
},
"prod":
{
"data": "1"
},
}
我像这样加载json:
open(FH,"<",$file) or die "environments.json file doesn't exists!\n";
my $data = <FH>;
my $ret = JSON::decode_json( $data );
正确加载,我可以这样做:
print "Dumper: " . Dumper($ret) . "\n";
但是当我尝试使用:
进行循环时foreach my $env( @$ret )
{
print "Environment: " . $env->{"data"} . "\n";
}
我明白了:
Not an ARRAY reference at ./launcher.pl line 20, <FH> line 1.
在foreach my $env( @$ret )
行
感谢帮助!
谢谢!
大卫
答案 0 :(得分:2)
JSON中没有数组。
for my $env_name (keys(%$ret)) {
my $env = $ret->{$env_name};
... $env->{data} ...
}