我有这个字符串,我需要转换为哈希:
str = '{:gender=>{:content=>"male"}, :names=>[{:first=>"Atair", :last=>"Goron"}], :addresses=>[{:country=>"GB", :state=>"ENG", :city=>"London"}], :emails=>[{:address=>"agoron@keolis.com", :address_md5=>"971b2fd0b83c900a5ca62c91dab80dd9"}, {:address=>"atair.godon.75@facebook.com", :address_md5=>"a0d958c1396300a344356d57b6836f9e"}], :jobs=>[{:title=>"Chief Executive Officer", :organization=>"Kolis UK and Eire"}, {:title=>"Deputy Managing Director", :organization=>"Kolis UK", :date_range=>{:start=>"2008-04-01", :end=>"2010-04-01"}}, {:title=>"Project Director", :organization=>"Keolis UK", :date_range=>{:start=>"2004-04-01", :end=>"2008-04-01"}}, {:title=>"Strategic Planning Director", :organization=>"Eurostar", :date_range=>{:start=>"2002-04-01", :end=>"2004-04-01"}}, {:title=>"Commercial Director, Business Markets", :organization=>"Eurostar", :date_range=>{:start=>"1999-11-01", :end=>"2002-04-01"}}, {:title=>"Brand Manager", :organization=>"Eurostar", :date_range=>{:start=>"1998-06-01", :end=>"1999-11-01"}}, {:title=>"Business Forecasting Manager", :organization=>"Eurostar", :date_range=>{:start=>"1997-08-01", :end=>"1998-06-01"}}, {:title=>"Principal Consultant", :organization=>"Steer Davies Gleave", :date_range=>{:start=>"1995-04-01", :end=>"1997-08-01"}}, {:title=>"Transport Consultant", :organization=>"Sir Alexander Gibb and Part", :date_range=>{:start=>"1990-01-01", :end=>"1995-12-31"}}], :educations=>[{:school=>"U. of London"}, {:school=>"University College London, U. of London", :date_range=>{:start=>"1987-01-01", :end=>"1990-12-31"}}], :usernames=>[{:content=>"alistair.gordon.75"}], :user_ids=>[{:content=>"5/238/746@linkedin"}, {:content=>"538818605@facebook"}, {:content=>"15504678@linkedin"}]}'
我有浮动的键数,每个键值可以是字符串,数组,散列或哈希散列
有转换它的方法吗?
您可以尝试通过替换:key=>
为"key":
以下格式将格式转换为JSON :
str.gsub!(/:(\w+)=>/, '"\1":') #=> "{\"gender\":{\"content\":\"male\"}, \"names\":[{\"first\":\"Atair\", ...
并像往常一样解析它:
require 'json' JSON.parse(str, symbolize_names: true) #=> {:gender=>{:content=>"male"}, :names=>[{:first=>"Atair", ...
这是脏方式 eval:eval
用字符串计算Ruby表达式.
> s = '{:gender=>{:content=>"male"}, :names=>[{:first=>"Atair", :last=>"Goron"}], :addresses=>[{:country=>"GB", :state=>"ENG", :city=>"London"}], :emails=>[{:address=>"agoron@keolis.com", :address_md5=>"971b2fd0b83c900a5ca62c91dab80dd9"}, {:address=>"atair.godon.75@facebook.com", :address_md5=>"a0d958c1396300a344356d57b6836f9e"}], :jobs=>[{:title=>"Chief Executive Officer", :organization=>"Kolis UK and Eire"}, {:title=>"Deputy Managing Director", :organization=>"Kolis UK", :date_range=>{:start=>"2008-04-01", :end=>"2010-04-01"}}, {:title=>"Project Director", :organization=>"Keolis UK", :date_range=>{:start=>"2004-04-01", :end=>"2008-04-01"}}, {:title=>"Strategic Planning Director", :organization=>"Eurostar", :date_range=>{:start=>"2002-04-01", :end=>"2004-04-01"}}, {:title=>"Commercial Director, Business Markets", :organization=>"Eurostar", :date_range=>{:start=>"1999-11-01", :end=>"2002-04-01"}}, {:title=>"Brand Manager", :organization=>"Eurostar", :date_range=>{:start=>"1998-06-01", :end=>"1999-11-01"}}, {:title=>"Business Forecasting Manager", :organization=>"Eurostar", :date_range=>{:start=>"1997-08-01", :end=>"1998-06-01"}}, {:title=>"Principal Consultant", :organization=>"Steer Davies Gleave", :date_range=>{:start=>"1995-04-01", :end=>"1997-08-01"}}, {:title=>"Transport Consultant", :organization=>"Sir Alexander Gibb and Part", :date_range=>{:start=>"1990-01-01", :end=>"1995-12-31"}}], :educations=>[{:school=>"U. of London"}, {:school=>"University College London, U. of London", :date_range=>{:start=>"1987-01-01", :end=>"1990-12-31"}}], :usernames=>[{:content=>"alistair.gordon.75"}], :user_ids=>[{:content=>"5/238/746@linkedin"}, {:content=>"538818605@facebook"}, {:content=>"15504678@linkedin"}]}' > eval(s) #=> {:gender=>{:content=>"male"}, :names=>[{:first=>"Atair", :last=>"Goron"}], :addresses=>[{:country=>"GB", :state=>"ENG", :city=>"London"}], :emails=>[{:address=>"agoron@keolis.com", :address_md5=>"971b2fd0b83c900a5ca62c91dab80dd9"}, {:address=>"atair.godon.75@facebook.com", :address_md5=>"a0d958c1396300a344356d57b6836f9e"}], :jobs=>[{:title=>"Chief Executive Officer", :organization=>"Kolis UK and Eire"}, {:title=>"Deputy Managing Director", :organization=>"Kolis UK", :date_range=>{:start=>"2008-04-01", :end=>"2010-04-01"}}, {:title=>"Project Director", :organization=>"Keolis UK", :date_range=>{:start=>"2004-04-01", :end=>"2008-04-01"}}, {:title=>"Strategic Planning Director", :organization=>"Eurostar", :date_range=>{:start=>"2002-04-01", :end=>"2004-04-01"}}, {:title=>"Commercial Director, Business Markets", :organization=>"Eurostar", :date_range=>{:start=>"1999-11-01", :end=>"2002-04-01"}}, {:title=>"Brand Manager", :organization=>"Eurostar", :date_range=>{:start=>"1998-06-01", :end=>"1999-11-01"}}, {:title=>"Business Forecasting Manager", :organization=>"Eurostar", :date_range=>{:start=>"1997-08-01", :end=>"1998-06-01"}}, {:title=>"Principal Consultant", :organization=>"Steer Davies Gleave", :date_range=>{:start=>"1995-04-01", :end=>"1997-08-01"}}, {:title=>"Transport Consultant", :organization=>"Sir Alexander Gibb and Part", :date_range=>{:start=>"1990-01-01", :end=>"1995-12-31"}}], :educations=>[{:school=>"U. of London"}, {:school=>"University College London, U. of London", :date_range=>{:start=>"1987-01-01", :end=>"1990-12-31"}}], :usernames=>[{:content=>"alistair.gordon.75"}], :user_ids=>[{:content=>"5/238/746@linkedin"}, {:content=>"538818605@facebook"}, {:content=>"15504678@linkedin"}]}
注意:正如Drenmi评论我也同意如果可能的话,使用时会有一些安全隐患.我建议如果可能检索响应而不是字符串.它执行它传递的任何东西,你必须110%肯定(因为,至少没有用户输入的任何地方)它将只包含正确形成的哈希或来自外太空的意外错误/可怕的生物可能会开始弹出.eval
eval
json