4

I've got a JSON string. I need to replace some values in it. I do it this way:

string jsonString = "{\"id\": \"5281959998_126883980715630\", \"name\": \"The New York Times\", \"category\": \"Company\"}";
JObject jObj = JObject.Parse(jsonString);
jObj["category"] = "inc";
string strJson = jObj.ToString();

But it doesn't work properly! Symbols "\r\n" appears after each key-value pair. What am I doing wrong? How can I prevent appearance of these symbols?

2
  • 2
    Given that it's still valid JSON with the same meaning, why do you really care? You can always just replace "\r\n" with "" if you really want to. Commented Sep 27, 2012 at 11:32
  • 1
    @JonSkeet: Or use the correct method... Commented Sep 27, 2012 at 11:37

2 Answers 2

6

Using ToString() you can't change this behaviour. It is documented that this method returns indented JSON.

You need to use the overload instead:

var result = jObj.ToString(Formatting.None);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, I completely forgot about it!
1

In fact with \r\ns or not, they are valid jsons.

string strJson = jObj.ToString(Newtonsoft.Json.Formatting.None, null);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.