1

I have a string like this coming from an API:

 "{\"region\":{\"span\":{\"latitude_delta\":0.11197800000000058,\"longitude_delta\":0.10020299999999338},.....

What should I do in order to be able to access my_returned_object.region? Right now it's just a string, I don't know how to convert it to another object type.

user229044
  • 222,134
  • 40
  • 319
  • 330
Hommer Smith
  • 24,944
  • 53
  • 156
  • 273

2 Answers2

3

It's a string containing encoded JSON.

You need to install and use a JSON parser to turn it into a hash.

First, install the gem:

gem install json

Then use it:

require 'json'

data = JSON.parse("{\"region\":{\"span\":{\"latitude_delta\":0.11197800000000058,\"longitude_delta\":0.10020299999999338}}}")

puts data["region"]
Community
  • 1
  • 1
user229044
  • 222,134
  • 40
  • 319
  • 330
0

This seems to be a JSON encoded object. You could try

require 'json'
my_hash = JSON.load(my_returned_object).symbolize_keys
puts my_hash[:region]

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.

oldergod
  • 14,813
  • 7
  • 57
  • 84