Alex Rivera | Logout

Handling newlines in data with Json.Net

Asked 2012-10-30T17:07:04.620
9

Is there any way to handle newlines in JSON.NET. I have some data coming back with Carriage Return Line Feed in it and Json.Net is just leaving it raw in the return value. Is there a way to force Json.Net to encode this for Json. I assumed this would happen by default but it is not happening for me. Maybe I am missing something else.

I am using Json.Net in a MVC4 WebApi project if that matters.

My data is coming back with \r\n in the string such as

  "Keywords": "These are my keywords.\r\n\r\n\r\nThis is a second line...\r\n\r\nThis is a third line. ...\r\n\r\n\r\nThis is a 4th line ..."

From what I understand, that should be \\r\\n. It could be a problem with the data I am returning, but I just wanted to see what JSON.NET should be doing with this.

Edit
Report

1 Answer

-5

You can serialize your object with the option Formatting.Indented. Like this:

string yourJsonString = JsonConvert.SerializeObject(yourObject,  Formatting.Indented, new JsonSerializerSettings { });

I think this should work.

Regards.

answered 2012-10-30T17:18:21.437

Your Answer