views:

56

answers:

1

Hi

I send this json to my controller (as seen in my webrick log):

{\"repeatEveryYear\":\"TRUE\",\"scheduleTime\":\"2010-09-09T16:11:46Z\",\"message\":\"Hello World\n\nFirst test\"}

I dont know where all the escaping comes from, it is not something I add, and it seems rails eats it just fine?

My problem is that the second \n gets eaten some where in the process. Here is my controller that reads the json:

class SchedulesController < ApplicationController
  def create
    @schedule = Schedules.new.from_json(params[:schedule])
    @schedule.save
    render :json => "ok"
  end
end

Any ideas on what I can do to fix this?

Thank you

A: 

You might have to double-escape the newline characters:

{\"repeatEveryYear\":\"TRUE\",\"scheduleTime\":\"2010-09-09T16:11:46Z\",\"message\":\"Hello World\\n\\nFirst test\"}

This will convert each \\ to \ on the first pass (first decode) and then the \n to the newline on the 2nd pass (since it looks like it's a two-pass string).

Vermiscious
Well then why is it only one of the double \n that gets eaten? Hm maybe, but how do I do this? I have tried with .gsub(/\n/, "\\n")
Neigaard
Just did some more testing. If I only have one \n in the json, like A\nB, it also gets removed and turned into a space so it becomes A B, but if I have A\n\nB it becomes A\nB??
Neigaard
Hm well the .gsub(/\n/, "\\n") does work...
Neigaard