I’m trying to convert the following string to either a hash or json.
How do I do this in ruby?
[{"place":null,"coordinates":null,"in_reply_to_user_id":null,"in_reply_to_status_id":null,
"favorited":false,"truncated":false,"created_at":"Wed Nov 16 08:00:46 +0000 2011","retweet_count":0,"in_reply_to_screen_name":null,
"user":{"profile_background_image_url":"http:\/\/a1.twimg.com\/profile_background_images\/190989640\/afcx.jpg","protected":false,
"statuses_count":23414,"profile_link_color":"FF0000"},"retweeted":false,"in_reply_to_status_id_str":null,"in_reply_to_user_id_str":null,"contributors":null,"geo":null}]
I’m running ruby1.8.7.
What you have appears to be JSON already, so I assume you’re looking to get a Ruby Hash from it. If so, then this should work:
Get a JSON library, I used
gem install json_pure, which is a native Ruby implementation (there’s a faster, C-based version but you wouldn’t notice the difference unless your JSON strings are very large or you have a lot of them).then
Note that the string you gave is a single-element array containing something that will map to a Ruby Hash. If you just want the hash:
I get this: