gpt4 book ai didi

json - 将 Perl 字符串转换为 json 数组

转载 作者:行者123 更新时间:2023-12-02 14:46:15 26 4
gpt4 key购买 nike

我正在尝试编写一个将在 Centos 6 主机上运行的 Perl 脚本。该脚本将运行一个 shell 命令来查询我们的 AWS 接口(interface),如下所示:

my $json = `aws ec2 describe-instances`;

此查询将返回一个 JSON 对象,但我相信我的问题是它实际上作为字符串存储在 $output 中,而不是实际存储在 JSON 对象中。

我正在尝试以下代码:

# Get each instance
my $json = `aws ec2 describe-instances`;

# Decode AWS json result
my $decoded = decode_json $json;

# Find reference type
print "Reference type: " . ref $decoded,"\n";

# Loop through each EC2 instance
print $decoded -> {Reservations} . "\n";

当我执行此代码时,我收到以下消息:

Reference type: HASH
ARRAY(0xa774b0)

有人可以帮我解决这个问题吗?我已经在谷歌上搜索并搞乱了这一整天了:(

下面是存储在 $output 中的字符串示例,尽管在实际输出中会有多个实例:

{
"Reservations": [
{
"OwnerId": "82XXXXXXX043",
"ReservationId": "r-0XXXXXXXXXXX",
"Groups": [],
"Instances": [
{
"Monitoring": {
"State": "XXXXX"
},
"PublicDnsName": "XXX",
"RootDeviceType": "XXXXXXX",
"State": {
"Code": XXX,
"Name": "stopped"
},
"EbsOptimized": XXXX,
"LaunchTime": "XXXXXXXXXX",
"PrivateIpAddress": "XXXXXXXXXX",
"ProductCodes": [],
"VpcId": "XXXXXXXXXX",
"StateTransitionReason": "UXXXXXXXXXX",
"InstanceId": "XXXXXXXXXX",
"ImageId": "XXXXXXXXXX",
"PrivateDnsName": "XXXXXXXXXX",
"KeyName": "XXXXXXXXXX",
"SecurityGroups": [
{
"GroupName": "XXXXXXXXXX",
"GroupId": "XXXXXXXXXX"
},
{
"GroupName": "XXXXXXXXXX",
"GroupId": "XXXXXXXXXX"
},
{
"GroupName": "XXXXXXXXXX",
"GroupId": "XXXXXXXXXX"
}
],
"ClientToken": "XXXXXXXXXX",
"SubnetId": "XXXXXXXXXX",
"InstanceType": "XXXXXXXXXX",
"NetworkInterfaces": [
{
"Status": "XXXXXXXXXX",
"MacAddress": "XXXXXXXXXX",
"SourceDestCheck": XXXXXXXXXX,
"VpcId": "XXXXXXXXXX",
"Description": "XXXXXXXXXX",
"NetworkInterfaceId": "XXXXXXXXXX",
"PrivateIpAddresses": [
{
"Primary": XXXXXXXXXX,
"PrivateIpAddress": "XXXXXXXXXX"
}
],
"Ipv6Addresses": [],
"Attachment": {
"Status": "XXXXXXXXXX",
"DeviceIndex": 01234,
"DeleteOnTermination": XXXXXXXXXX,
"AttachmentId": "XXXXXXXXXX",
"AttachTime": "XXXXXXXXXX"
},
"Groups": [
{
"GroupName": "XXXXXXXXXX",
"GroupId": "XXXXXXXXXX"
},
{
"GroupName": "XXXXXXXXXX",
"GroupId": "XXXXXXXXXX"
},
{
"GroupName": "XXXXXXXXXX",
"GroupId": "XXXXXXXXXX"
}
],
"SubnetId": "XXXXXXXXXX",
"OwnerId": "XXXXXXXXXX",
"PrivateIpAddress": "XXXXXXXXXX"
}
],
"SourceDestCheck": XXXXXXXXXX,
"Placement": {
"Tenancy": "XXXXXXXXXX",
"GroupName": "XXXXXXXXXX",
"AvailabilityZone": "XXXXXXXXXX"
},
"Hypervisor": "XXXXXXXXXX",
"BlockDeviceMappings": [
{
"DeviceName": "XXXXXXXXXX",
"Ebs": {
"Status": "XXXXXXXXXX",
"DeleteOnTermination": XXXXXXXXXX,
"VolumeId": "XXXXXXXXXX",
"AttachTime": "XXXXXXXXXX"
}
}
],
"Architecture": "XXXXXXXXXX",
"StateReason": {
"Message": "XXXXXXXXXX",
"Code": "XXXXXXXXXX"
},
"IamInstanceProfile": {
"Id": "XXXXXXXXXX",
"Arn": "XXXXXXXXXX"
},
"RootDeviceName": "XXXXXXXXXX",
"VirtualizationType": "XXXXXXXXXX",
"Tags": [
{
"Value": "XXXXXXXXXX",
"Key": "XXXXXXXXXX"
},
{
"Value": "XXXXXXXXXX",
"Key": "XXXXXXXXXX"
},
{
"Value": "XXXXXXXXXX",
"Key": "XXXXXXXXXX"
},
{
"Value": "XXXXXXXXXX",
"Key": "XXXXXXXXXX"
},
{
"Value": "XXXXXXXXXX",
"Key": "XXXXXXXXXX"
},
{
"Value": "XXXXXXXXXX",
"Key": "XXXXXXXXXX"
}
],
"AmiLaunchIndex": XXXXXXXXXX
}
]
}
]
}

最佳答案

您的 JSON 以 { 开头,因此错误是完全正确的。您没有数组引用,而是哈希引用。

尝试 print ref $decoded,"\n" 它会告诉您您拥有的引用类型。

尝试 $decoded -> {Reservations} 或使用 Data::Dumper 打印整个内容:

use Data::Dumper;
print Dumper $decoded;

关于json - 将 Perl 字符串转换为 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50024685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com