Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert JSON object to a string which has no formatting and indentation

I'm using the Json.NET (Newtonsoft.Json) package in a Portable Class Library (PCL) project (targetting Xamarin.Android and Xamarin.iOS) and would like to get a string representation of a JSON object with no formatting (i.e. no new lines, no tabs etc). How can I do this?

Currently, if I call JObject.ToString() on a JObject instance, I get a string with the new line (\n) character as follows:

"{\n  \"key\": \"value\"\n}"

Essentially, what I'd like to do is parse an initial string representation of a JSON object which or may not contain formatting/indentation/etc, convert the parsed JSON object to a string which does not contain formatting/indentation/etc, and end with a string something as follows:

"{\"key\":\"value\"}"

Is this possible with the Json.NET (Newtonsoft.Json) package in a PCL project? Is there another library I could use to accomplish this?

like image 782
Adil Hussain Avatar asked Sep 15 '25 19:09

Adil Hussain


1 Answers

Have you tried the overload of JObject.ToString() which accepts a Formatting enum value?

string json = jObject.ToString(Formatting.None);
like image 109
Brian Rogers Avatar answered Sep 18 '25 09:09

Brian Rogers