ruby on rails - How to convert string to hash WITHOUT EVAL -



ruby on rails - How to convert string to hash WITHOUT EVAL -

this question has reply here:

how convert string object hash object? 11 answers

i have string. {"response_code"=>"success", "authorisation_result"=>"approved", "transaction_number"=>"1234567", "receipt_number"=>"999999", :cents=>100} stored value in hstore in postgresql.

so it's raw string not yaml or json. wonering how value hash used insert record.

the thought of using eval scares hell out of me, because there potential utilize input here.

question might seem replica of how convert string hash in ruby/rails without using eval?, reply there doesn't mesh question. offers eval solution. (i don't understand how reply marked answer)

ruby hashes pretty similar json, work example:

require 'json' str = "{\"response_code\"=>\"success\", \"authorisation_result\"=>\"approved\", \"transaction_number\"=>\"1234567\", \"receipt_number\"=>\"999999\", :cents=>100}" json.parse str.gsub(/:(\w+)/){"\"#{$1}\""}.gsub('=>', ':') # => {"response_code"=>"success", "authorisation_result"=>"approved", "transaction_number"=>"1234567", "receipt_number"=>"999999", "cents"=>100}

ruby-on-rails postgresql hstore

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -