2013-03-12 55 views
0
"{\"status\":1,\"redirect\":\"/some/uri/uri2/index.html?post_login=80607979823520\",\"security_token\":\"/cpsess8233434446\"}" 

我得到這個響應作爲字符串,我需要提取security_token值。 我試圖通過eval method.seems將字符串轉換爲哈希不工作,我需要做一個正則表達式匹配。正則表達式提取子串紅寶石

回答

2

你可以這樣做:

require 'json' 
a = JSON.load "{\"status\":1,\"redirect\":\"/some/uri/uri2/index.html?post_login=80607979823520\",\"security_token\":\"/cpsess8233434446\"}" 
p a["security_token"] #=> "/cpsess8233434446" 
2

您需要解析JSON數據..

result = "{\"status\":1,\"redirect\":\"/some/uri/uri2/index.html?post_login=80607979823520\",\"security_token\":\"/cpsess8233434446\"}" 
h = JSON.parse(result) 
h['security_token']  # => "/cpsess8233434446" 
1

您可以JSON.load數據和過濾器[ 'security_token']或使用.match(/ security_token /)風格的正則表達式。

我會建議事先未來的可讀性和代碼維護。