Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing values in JSON string using JSON.net library (C#)

Tags:

c#

json.net

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?

like image 831
Sergey Shafiev Avatar asked Oct 24 '25 19:10

Sergey Shafiev


1 Answers

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);
like image 109
Daniel Hilgarth Avatar answered Oct 26 '25 08:10

Daniel Hilgarth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!