How to parse String to Hash in Ruby

Parsing JSON

To parse a JSON string received by another application or generated within your existing application:

require 'json'

my_hash = JSON.parse('{"hello": "goodbye"}')
puts my_hash["hello"] => "goodbye"
require 'rubygems'
require 'json'

content = %Q({\"credentials\"=>{\"token\"=>\"F538FF45937887AF7246E50928E0961F1\", \"refresh_token\"=>\"6EC400DD7B547B401D29474EA68952145\", \"expires_at\"=>1418310654, \"expires\"=>true}})

hash = JSON.parse content.gsub!('=>', ':')

puts hash.class
#=> Hash

Notice the extra quotes ‘’ around the hash notation. Ruby expects the argument to be a string and can’t convert objects like a hash or array.

Ruby converts your string into a hash.

Generating JSON

require 'json'

my_hash = {:hello => "goodbye"}
puts JSON.generate(my_hash) => "{\"hello\":\"goodbye\"}"

Or an alternative way:

require 'json'
puts {:hello => "goodbye"}.to_json => "{\"hello\":\"goodbye\"}"

如果觉得我的文章对您有用,请在支付宝公益平台找个项目捐点钱。 @Victor Sep 15, 2014

奉献爱心