我有这个字符串:如何将序列化的数组字符串转换为数组?
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