Newtonsoft JObject adding newline characters when converting to string

71 Views Asked by At

I have a C# program that adds properties to a JObject but when I convert to string, it has \r and \n characters in the output. I'm using a very simple example with only the name and value:

JObject jo = new JObject();
jo.Add("role", "user");
jo.ToString() //Output is:  "{\r\n  \"role\": \"user\"\r\n}"

However if I do the following:

jo["role"].ToString()  // output is: "user"

So, how do I stop it from adding the /r/n when converting to string?

1

There are 1 best solutions below

0
Velocedge On

Just after I posted the question, I found this so I thought I'd post in case anyone else has the problem.

jo.ToString(Newtonsoft.Json.Formatting.None);