Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在perl中将简单的perl字符串转换为JSON_Json_Perl - Fatal编程技术网

在perl中将简单的perl字符串转换为JSON

在perl中将简单的perl字符串转换为JSON,json,perl,Json,Perl,我对JSON很陌生。我运行了一些命令并将其输出存储在字符串中。现在我想把它转换成JSON。如何将其转换为perl哈希引用,然后将其转换为JSON。我的输出如下,但这是字符串格式:- {"limits": {"rate": [], "absolute": {"maxServerMeta": 128, "maxPersonality": 5, "maxImageMeta": 128, "maxPersonalitySize": 10240, "maxSecurityGroupRules": 20,

我对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;

如何解决

您的$curl\u cmd3\u输出是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
使用严格;
使用警告;
使用JSON;
我的$curl\u cmd3\u输出=q!{“限制”:{“速率”:[],“绝对”:{“maxServerMeta”:128,“maxPersonality”:5,“MaxImageTA”:128,“maxPersonalitySize”:10240,“maxSecurityGroupRules”:20,“maxTotalKeypairs”:100,“totalRAMUsed”:6144,“TotalInstanceUsed”:3,“maxSecurityGroups”:10,“totalFloatingIpsUsed”:0,“maxTotalCores”:20,“totalSecurityGroupsUsed”:0,“maxTotalFloatingIps”:10,“maxTotalInstances”:10,“totalCoresUsed”:6,“maxTotalRAMSize”:51200}}!;

my$h=from_json($curl_cmd3_output);#{limits}->{absolute}->{maxSecurityGroupRules},“\n”;#i$curl_cmd3_output是命令的输出。因此我无法像您那样硬编码。我必须传递字符串或在q!{}中运行命令!;请告诉我如何在此中传递字符串。您试图将字符串转换为JSON是什么意思?@Miller根据下面的解决方案,我首先需要将字符串转换为perl哈希。然后获取值。好吧……您的问题措辞不当,因为您的字符串已经是JSON。不过,如果下面的解决方案之一能够回答这是你的问题,请随意给他们打勾。
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