Hello all, I have a question regarding the to_json function. If I create a string in script/console, and then apply the to_json method, it will take the first and last double quote as part of the string. Is this behaviour normal? eg: test = "my string" test.to_json will return "\"my string\"" Regards, Carl --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ball, Donald A Jr \(Library\)
2007-Apr-13 16:53 UTC
Re: to_json adds double quote to beginning and end of string
> I have a question regarding the to_json function. If I create > a string in script/console, and then apply the to_json > method, it will take the first and last double quote as part > of the string. Is this behaviour normal? > > eg: > > test = "my string" > test.to_json will return "\"my string\""Yep. The rails console is printing test.to_json as a string value like any other. Notice if you output the test variable itself, being a string value, it''s surrounded by double quotes. The same thing happens to test.to_json, with the added wrinkle that the value contains actual double quote characters, which are escaped for display. - donald --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
eden li
2007-Apr-15 08:59 UTC
Re: to_json adds double quote to beginning and end of string
It''s not taking the first and last double quote as part of the string, per say. It''s just generating a valid JSON object for you. In the case of a bare words like: my string, the JSON would be "my string".>> ''blah''.to_json=> "\"blah\"">> puts ''blah''.to_json"blah" => nil On Apr 14, 12:39 am, "Carl H." <charr...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello all, > > I have a question regarding the to_json function. If I create a string > in script/console, and then apply the to_json method, it will take the > first and last double quote as part of the string. Is this behaviour > normal? > > eg: > > test = "my string" > test.to_json will return "\"my string\"" > > Regards, > Carl--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---