我有這個字符串:如何將序列化的數組字符串轉換爲數組?
temp = "["minutes", "hours"]"
如果我這樣做:
temp[1..-2].split(", ")
我得到2個元素這樣的數組:
[0] = ""minutes""
[1] = ""hours""
我怎樣才能避免有雙報價?
我有這個字符串:如何將序列化的數組字符串轉換爲數組?
temp = "["minutes", "hours"]"
如果我這樣做:
temp[1..-2].split(", ")
我得到2個元素這樣的數組:
[0] = ""minutes""
[1] = ""hours""
我怎樣才能避免有雙報價?
還有一個:
the_string.scan(/\"(\w+)\"/).flatten
=> ["minutes", "hours"]
使用JSON解析器:
JSON.parse(your_array)
只要做到:
temp.gsub("\"", "")[1..-2].split(", ")
或者,一旦你有每個元素雙引號的數組:
temp.map{|e| e.squeeze("̣\"")}
看起來它來自JSON。也許你應該使用JSON解析器? – 2013-03-03 16:03:41
'需要'json''和'JSON.parse溫度' – 2013-03-03 16:04:34
http://stackoverflow.com/questions/5410682/parsing-a-json-string-in-ruby – 2013-03-03 16:06:06