1

I have a string, which my app reads from a remote API source (Facebook graph), that looks like:

s = '{
     "id": "123456",
     "name": "App name",
     "link": "http://www.example.com",
     "location": {
       "street": "123 Main St",
       "city": "Anytown",
       "state": "XX"
      },
     "username": "platform"
    }'

Is there a simple method to create a hash from that string?

2
  • 1
    Are you sure this is the input? If you didn't have the last comma, you could have used JSON to parse. Commented Aug 9, 2011 at 16:51
  • you're right about the comma, which I just fixed, (I removed a lot of lines so it was shorter example). How would I use JSON to parse that? Commented Aug 9, 2011 at 16:54

1 Answer 1

6

Facebook Graph API returns a JSON document, that you can parse using JSON.parse.

s = '{
     "id": "123456",
     "name": "App name",
     "link": "http://www.example.com",
     "location": {
       "street": "123 Main St",
       "city": "Anytown",
       "state": "XX"
      },
     "username": "platform"
    }'
JSON.parse(s)

Output:

=> {"id"=>"123456", "name"=>"App name", "link"=>"http://www.example.com", "locat
ion"=>{"street"=>"123 Main St", "city"=>"Anytown", "state"=>"XX"}, "username"=>"
platform"}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.