当前位置:  开发笔记 > 编程语言 > 正文

字符串哈希哈希/数组

如何解决《字符串哈希哈希/数组》经验,为你挑选了2个好方法。

我有这个字符串,我需要转换为哈希:

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"}]}'

我有浮动的键数,每个键值可以是字符串,数组,散列或哈希散列

有转换它的方法吗?



1> Stefan..:

您可以尝试通过替换: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", ...



2> Gagan Gami..:

这是脏方式 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"}]} 

DEMO

注意:正如Drenmi评论我也同意如果可能的话,使用时会有一些安全隐患.我建议如果可能检索响应而不是字符串.它执行它传递的任何东西,你必须110%肯定(因为,至少没有用户输入的任何地方)它将只包含正确形成的哈希或来自外太空的意外错误/可怕的生物可能会开始弹出.evalevaljson


还没有downvotes.:-)使用`eval`有[安全隐患](http://stackoverflow.com/questions/637421/is-eval-supposed-to-be-nasty),这导致一些人完全拒绝它."eval is bad"口头禅从此变得有点不言而喻.
推荐阅读
mobiledu2402852357
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有