Having troubles extracting json data within javascript

View: New views
4 Messages — Rating Filter:   Alert me  

Having troubles extracting json data within javascript

by Srinath A. :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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.
--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Having troubles extracting json data within javascript

by Srinath A. :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


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

Is there a cleaner way of doing this?
--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Having troubles extracting json data within javascript

by Christopher Kintner :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Wed, Jul 23, 2008 at 10:44 PM, Chris Olsen
<rails-mailing-list@...> 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


> --
> Posted via http://www.ruby-forum.com/.
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Having troubles extracting json data within javascript

by Srinath A. :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Christopher Kintner wrote:

> On Wed, Jul 23, 2008 at 10:44 PM, Chris Olsen
> <rails-mailing-list@...> 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.
--
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@...
To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---