Ruby / Rhomobile:解析目录字符串表示

时间:2011-12-19 14:40:00

标签: ruby regex string parsing rhomobile


我有一个如下所示的字符串:{"whatever-field"=>"gghyduudud"}
我想解析它,以便它变成哈希。

请帮忙。

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用eval,但前提是数据源绝对可靠:

>> eval('{"whatever-field"=>"gghyduudud"}')
=> {"whatever-field"=>"gghyduudud"} 

答案 1 :(得分:0)

这是一个解决方案:

dictionary=Hash[*(dict_str[1..dict_str.length-2].split("=>").map {|strval| strval[1..strval.length-2]})]

只要您希望键和值为字符串,这将起作用。它有点长,但它对我有用。