如何在ruby中对json进行url编码

时间:2014-02-14 06:29:49

标签: php ruby json urlencode

我想使用ruby方法对收件人json进行编码,    以下是示例代码

recipients = 
{
"1JzSZFs2DQke2B3S4pBxaNaMzzVZaG4Cqh": 100000000,
"12Cf6nCcRtKERh9cQm3Z29c9MWvQuFSxvT": 1500000000,
"1dice6YgEVBf88erBFra9BHf6ZMoyvG88": 200000000
}

php中的url形成示例

<?

$guid="GUID_HERE";
$firstpassword="PASSWORD_HERE";
$secondpassword="PASSWORD_HERE";
$amounta = "10000000";
$amountb = "400000";
$addressa = "1A8JiWcwvpY7tAopUkSnGuEYHmzGYfZPiq";
$addressb = "1ExD2je6UNxL5oSu6iPUhn9Ta7UrN8bjBy";
$recipients = urlencode('{
              "'.$addressa.'": '.$amounta.',
              "'.$addressb.'": '.$amountb.'
           }');

$json_url = "http://blockchain.info/merchant/$guid/sendmany?password=$firstpassword&second_password=$secondpassword&recipients=$recipients";

如何使用ruby做这个urlencode?

2 个答案:

答案 0 :(得分:2)

使用URI.escape

require 'uri'

enc_uri = URI.escape("http://example.com/?a=\11\15")
p enc_uri
# => "http://example.com/?a=%09%0D"

答案 1 :(得分:0)

require 'uri'

enc_uri = URI.encode_base64("http://example.com/?a=\11\15")

p enc_uri