从 Ruby 中的 JSON 获取值
Get values from JSON in Ruby
我正在尝试使用 aws API 获取连接到机器的卷的 VolumeId 和状态。
代码
#!/usr/local/bin/ruby
require "aws-sdk"
require "rubygems"
list=Aws::EC2::Client.new(region: "us-east-1")
volume=list.describe_volumes()
volumes=%x( aws ec2 describe-volumes --region='us-east-1' )
puts volumes
下面是命令的示例输出
aws ec2 describe-volumes --region='us-east-1'
。
请帮助从下面
获取VolumeID和state
API(JSON) 的示例输出:
{
"Volumes": [
{
"AvailabilityZone": "us-east-1d",
"Attachments": [
{
"AttachTime": "2015-02-02T07:31:36.000Z",
"InstanceId": "i-bca66353",
"VolumeId": "vol-892a2acd",
"State": "attached",
"DeleteOnTermination": true,
"Device": "/dev/sda1"
}
],
"Encrypted": false,
"VolumeType": "gp2",
"VolumeId": "vol-892a2acd",
"State": "in-use",
"Iops": 100,
"SnapshotId": "snap-df910966",
"CreateTime": "2015-02-02T07:31:36.380Z",
"Size": 8
},
]
}
只得到 volume_ids
->
JSON.parse(volumes)['Volumes'].map{|v|v["VolumeId"]}
只得到 states
->
JSON.parse(volumes)['Volumes'].map{|v|v["state"]}
用于获取一个 hash/map,其中卷 ID 作为键,它们的状态作为值 ->
JSON.parse(volumes)['Volumes'].map{|v| [v["VolumeId"],v["state"]] }.to_h
我正在尝试使用 aws API 获取连接到机器的卷的 VolumeId 和状态。
代码
#!/usr/local/bin/ruby
require "aws-sdk"
require "rubygems"
list=Aws::EC2::Client.new(region: "us-east-1")
volume=list.describe_volumes()
volumes=%x( aws ec2 describe-volumes --region='us-east-1' )
puts volumes
下面是命令的示例输出
aws ec2 describe-volumes --region='us-east-1'
。
请帮助从下面
API(JSON) 的示例输出:
{
"Volumes": [
{
"AvailabilityZone": "us-east-1d",
"Attachments": [
{
"AttachTime": "2015-02-02T07:31:36.000Z",
"InstanceId": "i-bca66353",
"VolumeId": "vol-892a2acd",
"State": "attached",
"DeleteOnTermination": true,
"Device": "/dev/sda1"
}
],
"Encrypted": false,
"VolumeType": "gp2",
"VolumeId": "vol-892a2acd",
"State": "in-use",
"Iops": 100,
"SnapshotId": "snap-df910966",
"CreateTime": "2015-02-02T07:31:36.380Z",
"Size": 8
},
]
}
只得到 volume_ids
->
JSON.parse(volumes)['Volumes'].map{|v|v["VolumeId"]}
只得到 states
->
JSON.parse(volumes)['Volumes'].map{|v|v["state"]}
用于获取一个 hash/map,其中卷 ID 作为键,它们的状态作为值 ->
JSON.parse(volumes)['Volumes'].map{|v| [v["VolumeId"],v["state"]] }.to_h