处理来自 Aws::Route53::Client.new 的更好响应
Handling better response from Aws::Route53::Client.new
第一次尝试使用 Ruby AWS ADK V2,我正在尝试格式化我返回的数据,但似乎很难将其转换为可用格式。
我只想获取托管区域列表并显示在 table 中。
我有一个帮手:
def hosted_zones
r53 = Aws::Route53::Client.new
#convert to hash first so we can parse and covert to json
h = (r53.list_hosted_zones).to_hash
j = JSON.parse((h.to_json))
end
然后 returns 我以下 JSON:
{
"hosted_zones": [{
"id": "/hostedzone/Z1HSDGASSSME",
"name": "stagephil.com.",
"caller_reference": "2016-07-12T15:33:45.277646707+01:00",
"config": {
"comment": "Private DNS zone for stage",
"private_zone": true
},
"resource_record_set_count": 10
}, {
"id": "/hostedzone/ZJDGASSS0ZN3",
"name": "stagephil.com.",
"caller_reference": "2016-07-12T15:33:41.290143511+01:00",
"config": {
"comment": "Public DNS zone for stage",
"private_zone": false
},
"resource_record_set_count": 7
}],
"is_truncated": false,
"max_items": 100
}
我是 运行 一个真正但同时通过所有 hosted_zone 条目交互到 table.
的语句
这是获得响应的最佳方式,还是您可以请求响应已经 json?
为什么要将散列转换为 JSON,只是为了再次将其转换为散列? JSON.parse(some_hash.to_json)
只会给你 some_hash
.
也就是说,我认为不可能直接从 AWS 获得 JSON,主要是因为 their API responds with XML. I think that your solution is ideal if that's all you're doing, but if you want, you can make a request with an HTTP client and then take the XML that you receive and use something like ActiveSupport's Hash.from_xml 创建一个哈希,然后您可以将其转换为 JSON.
第一次尝试使用 Ruby AWS ADK V2,我正在尝试格式化我返回的数据,但似乎很难将其转换为可用格式。
我只想获取托管区域列表并显示在 table 中。
我有一个帮手:
def hosted_zones
r53 = Aws::Route53::Client.new
#convert to hash first so we can parse and covert to json
h = (r53.list_hosted_zones).to_hash
j = JSON.parse((h.to_json))
end
然后 returns 我以下 JSON:
{
"hosted_zones": [{
"id": "/hostedzone/Z1HSDGASSSME",
"name": "stagephil.com.",
"caller_reference": "2016-07-12T15:33:45.277646707+01:00",
"config": {
"comment": "Private DNS zone for stage",
"private_zone": true
},
"resource_record_set_count": 10
}, {
"id": "/hostedzone/ZJDGASSS0ZN3",
"name": "stagephil.com.",
"caller_reference": "2016-07-12T15:33:41.290143511+01:00",
"config": {
"comment": "Public DNS zone for stage",
"private_zone": false
},
"resource_record_set_count": 7
}],
"is_truncated": false,
"max_items": 100
}
我是 运行 一个真正但同时通过所有 hosted_zone 条目交互到 table.
的语句这是获得响应的最佳方式,还是您可以请求响应已经 json?
为什么要将散列转换为 JSON,只是为了再次将其转换为散列? JSON.parse(some_hash.to_json)
只会给你 some_hash
.
也就是说,我认为不可能直接从 AWS 获得 JSON,主要是因为 their API responds with XML. I think that your solution is ideal if that's all you're doing, but if you want, you can make a request with an HTTP client and then take the XML that you receive and use something like ActiveSupport's Hash.from_xml 创建一个哈希,然后您可以将其转换为 JSON.