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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Chris Olsen
2008-Jul-24 03:44 UTC
Re: Having troubles extracting json data within javascript
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Christopher Kintner
2008-Jul-24 14:19 UTC
Re: Having troubles extracting json data within javascript
On Wed, Jul 23, 2008 at 10:44 PM, Chris Olsen <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Chris Olsen
2008-Jul-24 17:05 UTC
Re: Having troubles extracting json data within javascript
Christopher Kintner wrote:> On Wed, Jul 23, 2008 at 10:44 PM, Chris Olsen > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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/evalJSONI 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---