views:

61

answers:

3

In Javascript you can access json as objects.

person = {
  name: {
    first: "Peter",
    last: "Parker"
  }
}

person.name.first

In ruby I have to use it like this:

person[:name][:first]

Is it possible to access json (and hash) as an object just like in javascript?

A: 

JavaScript uses object attributes as its implementation of associative arrays. So, using Ruby's hash type is basically doing the same thing.

Tim McNamara
How do I access it like an object like in Javascript?
never_had_a_name
+1  A: 

There is a json gem in ruby. Perhaps that will help.

http://flori.github.com/json/

TrentEllingsen
+1  A: 

You should check out the Hashie gem. It lets you do just what you are looking for. It has a Mash class which takes JSON and XML parsed hashes and gives you object-like access. It actually does deep-dives into the hash, converting any arrays or hashes inside the hash, etc.

http://github.com/intridea/hashie

Nicholas C