Rails:获取http响应的值

时间:2014-02-04 03:42:57

标签: ruby-on-rails ruby http

在rails中我发了一个api的帖子并得到了回复,但我不知道如何获取信息并找到“代码”变量的值。

$ response 
#<Net::HTTPOK 200 OK readbody=true>

$ puts response.body
{"success":true,"button":{"code":"dfhdsg7f23hjgfs7be7","type":"buy_now","style":"none","text":"Send to MeBrah","name":"MeBrah","description":"Coins you&#x27;re willing to give me.","custom":"6","callback_url":null,"success_url":null,"cancel_url":null,"info_url":null,"auto_redirect":false,"price":{"cents":40000000,"currency_iso":"BTC"},"variable_price":true,"choose_price":false,"include_address":false,"include_email":false}}

$ response.code
"200"  

我知道代码变量也是相同的“dfhdsg7f23hjgfs7be7”我只是好奇什么命令会返回它的值。

1 个答案:

答案 0 :(得分:5)

使用JSON.parse解析回复内容:

require 'json'

...

data = JSON.parse(response.body)
data['button']['code'] # => "dfhdsg7f23hjgfs7be7"