[英]How can I convert a hash representation in an escaped string to an actual hash?
I have a field stored in a MySQL db that is a string, but it is storing a representation of a hash.我有一个字段存储在 MySQL 数据库中,它是一个字符串,但它存储的是 hash 的表示。 I'm wondering how I could convert this string into a hash.
我想知道如何将此字符串转换为 hash。 I have tried a bunch of different things with gsub and JSON.parse to no avail.
我用 gsub 和 JSON.parse 尝试了一堆不同的东西,但无济于事。
Here is a reference to what I'm trying to convert:这是我要转换的内容的参考:
=> "{:address=>\"\", :city=>\"\", :country=>\"\", :zip=>\"\", :state=>\"\", :industry=>\"\", :org=>\"\", :job_title=>\"\", :purchasing_time_frame=>\"\", :role_in_purchase_process=>\"\", :no_of_employees=>\"\", :comments=>\"\", :custom_questions=>[{\"title\"=>\"License Number\", \"value\"=>\"345g3245\"}, {\"title\"=>\"License Type\", \"value\"=>\"Legal\"}], :create_time=>\"2022-01-17T22:49:26Z\"}"
The provided string is not JSON, it is a string representation of a hash that can converted to a hash with eval("{:address=>\"\", :city=>\"\", :country=>\"\", :zip=>\"\", :state=>\"\", :industry=>\"\", :org=>\"\", :job_title=>\"\", :purchasing_time_frame=>\"\", :role_in_purchase_process=>\"\", :no_of_employees=>\"\", :comments=>\"\", :custom_questions=>[{\"title\"=>\"License Number\", \"value\"=>\"345g3245\"}, {\"title\"=>\"License Type\", \"value\"=>\"Legal\"}], :create_time=>\"2022-01-17T22:49:26Z\"}")
提供的字符串不是 JSON,它是一个 hash 的字符串表示形式,可以转换为 Z0800FC577294C34E0B28AD283943\“5945Z 与
eval("{:address=>\"\", :city=>\"\", :country=>\"\", :zip=>\"\", :state=>\"\", :industry=>\"\", :org=>\"\", :job_title=>\"\", :purchasing_time_frame=>\"\", :role_in_purchase_process=>\"\", :no_of_employees=>\"\", :comments=>\"\", :custom_questions=>[{\"title\"=>\"License Number\", \"value\"=>\"345g3245\"}, {\"title\"=>\"License Type\", \"value\"=>\"Legal\"}], :create_time=>\"2022-01-17T22:49:26Z\"}")
Be kind, this is my first stack overflow answer.善良,这是我的第一个堆栈溢出答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.