我是JSON的新手。我运行了一些命令并将其输出存储在字符串中。现在我想将其转换为JSON。我如何将其转换为perl哈希引用,然后将我转换为JSON。我的输出是这样的,但这是字符串格式: -
{"limits": {"rate": [], "absolute": {"maxServerMeta": 128, "maxPersonality": 5, "maxImageMeta": 128, "maxPersonalitySize": 10240, "maxSecurityGroupRules": 20, "maxTotalKeypairs": 100, "totalRAMUsed": 6144, "totalInstancesUsed": 3, "maxSecurityGroups": 10, "totalFloatingIpsUsed": 0, "maxTotalCores": 20, "totalSecurityGroupsUsed": 0, "maxTotalFloatingIps": 10, "maxTotalInstances": 10, "totalCoresUsed": 6, "maxTotalRAMSize": 51200}}}
我正在使用此代码: -
my %hash_ref = split /[,:]/, $curl_cmd3_output;
my $h = from_json( $hash_ref ); #<-- $h is a perl hash reference
print $h;
$max= $h->{'limits'}{'absolute'}{'maxSecurityGroupRules'}, "\n"; #<-- 20
print $max;
但是我收到了这个错误
hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)
如何解决?
答案 0 :(得分:1)
$ curl_cmd3_output是JSON哈希的字符串表示形式。首先,您必须转换为perl哈希,然后读取您要查找的密钥:
use strict;
use warnings;
use JSON;
my $curl_cmd3_output = q!{"limits": {"rate": [], "absolute": {"maxServerMeta": 128, "maxPersonality": 5, "maxImageMeta": 128, "maxPersonalitySize": 10240, "maxSecurityGroupRules": 20, "maxTotalKeypairs": 100, "totalRAMUsed": 6144, "totalInstancesUsed": 3, "maxSecurityGroups": 10, "totalFloatingIpsUsed": 0, "maxTotalCores": 20, "totalSecurityGroupsUsed": 0, "maxTotalFloatingIps": 10, "maxTotalInstances": 10, "totalCoresUsed": 6, "maxTotalRAMSize": 51200}}}!;
my $h = from_json($curl_cmd3_output ); #<-- $h is a perl hash reference
print $h->{limits}->{absolute}->{maxSecurityGroupRules}, "\n"; #<-- 20