Hommer Smith
Hommer Smith

Reputation: 27852

Parsing string coming from API

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.

Upvotes: 1

Views: 108

Answers (2)

oldergod
oldergod

Reputation: 15010

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.

Upvotes: 0

user229044
user229044

Reputation: 239302

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"]

Upvotes: 3

Related Questions