Reputation: 10151
The following resp
is a string that is similar to hash in structure.
(rdb:1) p resp
"{\"_id\":\"4dd4eaa872f8be2d380000af\",\"account_id\":\"4dd0d71272f8be0499000009\",\"created_at\":\"2011-05-19T15:47:16+05:45\",\"line_id\":\"4dd4ea9d72f8be2d380000a5\",\"order\":{\"_id\":\"4dd4eaa872f8be2d380000b9\",\"amount\":1.2000000000000002,\"service_charge\":0.0},\"owner_id\":\"4dd0d71272f8be0499000008\",\"tenant_id\":\"4dca3f8e72f8be2950000003\",\"through_api\":true,\"title\":\"run name\",\"updated_at\":\"2011-05-19T15:47:16+05:45\"}"
How can I convert this into a hash?
Upvotes: 0
Views: 3877
Reputation: 121
require 'json'
hash_a = JSON.parse(resp)
p hash_a
{"through_api"=>true,
"created_at"=>"2011-05-19T15:47:16+05:45",
"title"=>"run name",
"updated_at"=>"2011-05-19T15:47:16+05:45",
"account_id"=>"4dd0d71272f8be0499000009",
"_id"=>"4dd4eaa872f8be2d380000af",
"owner_id"=>"4dd0d71272f8be0499000008",
"order"=>{"amount"=>1.2,
"_id"=>"4dd4eaa872f8be2d380000b9",
"service_charge"=>0.0},
"line_id"=>"4dd4ea9d72f8be2d380000a5",
"tenant_id"=>"4dca3f8e72f8be2950000003"}
Upvotes: 0
Reputation: 42893
This seems like a JSON encoded object. Try this:
require 'json'
p JSON.load(resp)
json
is part of Ruby 1.9, if you use 1.8 (or another Ruby implementation) you might need to install the json
gem using gem install json
.
Upvotes: 8