2014-02-14 52 views
3

欲編碼收件人JSON使用紅寶石方法, 下面是示例代碼,如何做JSON的URL編碼在紅寶石

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

示例URL形成在PHP,

<? 

$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

使用URI.escape

require 'uri' 

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

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

p enc_uri