JSON Deserialisation

Hi,

I have been playing with object.to_json to searilise my objects into
JSON notation. How does one deserailise these JSON strings back into
ruby objects?

Thanks,
GiantCranes

Giant C. wrote:

Hi,

I have been playing with object.to_json to searilise my objects into
JSON notation. How does one deserailise these JSON strings back into
ruby objects?

Thanks,
GiantCranes

Any ideas? I’m off on my holidays in a few hours and won’t have internet
access. I’d hate to have hack this without JSON.

var theSource = “{name:‘Gavin’, age:33}”;
eval( "theObject = " + theSource );
alert( theObject.name );
–> Gavin

Phrogz wrote:

var theSource = “{name:‘Gavin’, age:33}”;
eval( "theObject = " + theSource );
alert( theObject.name );
–> Gavin

I am trying to deserialise from JSON -> ruby.

How does one deserailise these JSON strings back into
ruby objects?

gem install ruby-json

Giant C. wrote:

Phrogz wrote:

var theSource = “{name:‘Gavin’, age:33}”;
eval( "theObject = " + theSource );
alert( theObject.name );
–> Gavin

I am trying to deserialise from JSON -> ruby.

Oops, sorry - didn’t read properly.

Victor G. wrote:

How does one deserailise these JSON strings back into
ruby objects?

gem install ruby-json

Thanks, I came upon that gem while searching google. I has assumed that
rails could handle it without the gem as it does serialisation.

Thanks.

On Dec 15, 2006, at 3:08 PM, Giant C. wrote:

Thanks.

If you gem install json or fjson then you can parse json into ruby

like this:

JSON.parse(jsonstring)

Cheers-
– Ezra Z.
– Lead Rails Evangelist
[email protected]
– Engine Y., Serious Rails Hosting
– (866) 518-YARD (9273)

Sorry, the to_json output from the ‘JSON’ library is:

“#Cart:0x2475448

not

“#”

as I stated above

Ezra Z. wrote:

On Dec 15, 2006, at 3:08 PM, Giant C. wrote:

Thanks.

If you gem install json or fjson then you can parse json into ruby
like this:

JSON.parse(jsonstring)

Cheers-
– Ezra Z.

Thanks for the reply.

When I install ‘JSON’ it seems to overload the to_json method. My active
record models now serialise as:

“#”

instead of the original to_json output of:

{“vehicle_ids”: [3, 6, 5]}

Does the ‘JSON’ library work with complex objects?

Thanks,
GiantCranes