开发者

Who is eating my \n

开发者 https://www.devze.com 2023-01-15 06:45 出处:网络
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 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?

Th开发者_开发技巧ank you


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消