访问嵌套的哈希值

时间:2013-11-11 13:59:59

标签: ruby hash

我有哈希:

request = {"AutoCreate"=>{"Response"=>{"Status"=>"OK", "StatusCode"=>"0", "TransactionStatus"=>"SUCCEEDED", "TransactionReference"=>"MxhRNvs5EuJLTEkhKqO5Olgk2zMtYMTOI0IvQInWPV8EYsWAI7Aji09B3B8Bigzkee20e3bc4bd9eab997d7825b883b7359"}}}

如何访问"Status""StatusCode""TransactionStatus"的哈希值?

3 个答案:

答案 0 :(得分:5)

result["AutoCreate"]["Response"]["Status"]
result["AutoCreate"]["Response"]["StatusCode"]
result["AutoCreate"]["Response"]["TransactionStatus"]

答案 1 :(得分:2)

访问状态

result['AutoCreate']['Response']['Status']

访问StatusCode

result['AutoCreate']['Response']['StatusCode']

和TransactionStatus

result['AutoCreate']['Response']['TransactionStatus']

答案 2 :(得分:1)

vals_to_get = ["Status", "StatusCode", "TransactionStatus"]
p request["AutoCreate"]["Response"].values_at(*vals_to_get) #=> ["OK", "0", "SUCCEEDED"]