I have searched in this group for a while, and it seems no one face the same problem like me yet. I am developing a rest web service(post method) that requires json data format from my javascript client application. I don''t know how to handle since I could not get data that is being sent. In my controller, params variable got messed with key and value. I want to know the way how to parse data from this to ruby object. Any idea is really appreciated. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Apr 2, 2009, at 3:40 AM, Chamnap wrote:> I have searched in this group for a while, and it seems no one face > the same problem like me yet. I am developing a rest web service(post > method) that requires json data format from my javascript client > application. I don''t know how to handle since I could not get data > that is being sent. In my controller, params variable got messed with > key and value. I want to know the way how to parse data from this to > ruby object. Any idea is really appreciated.Get the JSON gem for starters. sudo gem install json Then in your code: require ''rubygems'' gem ''json'' require ''json'' -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On Apr 2, 3:18 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote:> > Get the JSON gem for starters. > > sudo gem install json > > Then in your code: > require ''rubygems'' > gem ''json'' > require ''json''I have followed what you said, but still doesn''t work. The params variable still contains weird string like: {"{\"first_name\":\"chamnap \",\"last_name\":\"chhorn\"}"=>nil, "action"=>"create", "controller"=>"users"}. Therefore, I could not access through either params[:first_name] or params[:last_name]. Chamnap --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Apr 2, 2009, at 9:27 PM, Chamnap wrote:> > > > On Apr 2, 3:18 pm, Rob Biedenharn <R...-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org> wrote: >> >> Get the JSON gem for starters. >> >> sudo gem install json >> >> Then in your code: >> require ''rubygems'' >> gem ''json'' >> require ''json'' > > I have followed what you said, but still doesn''t work. The params > variable still contains weird string like: {"{\"first_name\":\"chamnap > \",\"last_name\":\"chhorn\"}"=>nil, "action"=>"create", > "controller"=>"users"}. Therefore, I could not access through either > params[:first_name] or params[:last_name]. > > Chamnapirb> require ''rubygems'' => true irb> gem ''json'' => true irb> require ''json'' => true irb> raw = "{\"first_name\":\"chamnap\",\"last_name\":\"chhorn\"}" => "{\"first_name\":\"chamnap\",\"last_name\":\"chhorn\"}" irb> puts raw {"first_name":"chamnap","last_name":"chhorn"} => nil irb> JSON(raw) => {"first_name"=>"chamnap", "last_name"=>"chhorn"} irb> cooked = JSON.parse(raw) => {"first_name"=>"chamnap", "last_name"=>"chhorn"} irb> raw => "{\"first_name\":\"chamnap\",\"last_name\":\"chhorn\"}" irb> cooked => {"first_name"=>"chamnap", "last_name"=>"chhorn"} irb> raw.class => String irb> cooked.class => Hash irb> cooked["first_name"] => "chamnap" irb> cooked[:first_name] => nil Note that JSON.parse is going to return a normal Ruby Hash, not a Rails ActiveSupport HashWithIndifferentAccess. You''ll have to use "first_name" as the key, not :first_name. Show the code that you''re trying to use that''s pulling that whole string into a key of the params hash. -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks I also have founded a method in ActiveSupport::JSON.decode, and it does work the same thing as you did show me. What do you think? Chamnap --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---