Having troubles extracting json data within javascript

I am having some difficulty extracting json data from a returned hash
table.

From the geocode data that is returned the following
console.log(data)
console.log(data[“latitude”])

Results in:
{“latitude”: “55.272766”, “longitude”: “-114.762116”}
undefined

Checking a javascript created hash
var foo = {“foo”: “footoyou”, “bar”: “barandbeyond”}
console.log(foo)
console.log(foo[“foo”])

Shows me
Object foo=footoyou bar=barandbeyond
footoyou

So obviously the json data is not is a json form, but rather just text.

Here is my controller code, where coords is the hash table
respond_to do |format|
format.js { render :json => coords.to_json, :layout => false}
end

I am getting the exact same output if I use :json => coords.to_json or
just :json => coords

Help is appreciated.

Just after I post I find that the following works
console.log(eval(’(’ + data + ‘)’))

Is there a cleaner way of doing this?

On Wed, Jul 23, 2008 at 10:44 PM, Chris O.
[email protected] wrote:

Just after I post I find that the following works
console.log(eval(’(’ + data + ‘)’))

Is there a cleaner way of doing this?

if you are using prototype you could use data.evalJSON(true)

See: http://www.prototypejs.org/api/string/evalJSON

Christopher Kintner wrote:

On Wed, Jul 23, 2008 at 10:44 PM, Chris O.
[email protected] wrote:

Just after I post I find that the following works
console.log(eval(’(’ + data + ‘)’))

Is there a cleaner way of doing this?

if you are using prototype you could use data.evalJSON(true)

See: http://www.prototypejs.org/api/string/evalJSON

I am using jQuery. On the json.org page there is some javascript
available that will do the trick. I figured that since json is such a
huge part of javascript that the functionality I was looking for was
already there, just hiding.

Thanks for the help.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs