14
["{\"id\":317277848652099585,\"Tweet text\":\"Food Carnival by KMS Homemade Food\\nOn the      occasion of this Holi and Good Friday, KMS Homemade Foods invites you...     http://t.co/2Y2mO6vr76\",\"Word count\":21,\"Url\":\"true\"}"]

a is a hash with some keys and values.

    a = a.to_json

converts the hash to a string. Now a is a string with all backslashes... I know tha

    puts a 

returns a string with all backslashes removed but what if i want to store the 'backslash removed string' in a variable?

4

3 回答 3

16

It's much better to use as_json instead. Provides the json without backslashes.

For reference, http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html

于 2015-12-08T17:58:12.057 回答
12

You can just gsub! to replace the \" with a single quote ', like so:

a.gsub!(/\"/, '\'')
于 2013-03-30T22:35:56.663 回答
0

I faced the same problem and I did eval(a) and it gave me actual hash with no double quotes and slashes although eval is considered security risk

于 2020-05-11T12:58:01.907 回答