what is the best way to convert a json formatted key value pair to ruby hash with symbol as key?

前端 未结 7 847
无人共我
无人共我 2020-12-23 13:08

I am wondering what is the best way to convert a json formatted key value pair to ruby hash with symbol as key: example:

{ \'user\': { \'name\': \'foo\', \'         


        
7条回答
  •  無奈伤痛
    2020-12-23 13:23

    using the json gem when parsing the json string you can pass in the symbolize_names option. See here: http://flori.github.com/json/doc/index.html (look under parse)

    eg:

    >> s ="{\"akey\":\"one\",\"bkey\":\"two\"}"
    >> JSON.parse(s,:symbolize_names => true)
    => {:akey=>"one", :bkey=>"two"} 
    

提交回复
热议问题