Newtonsoft.Json Serializeobject Without Escape Backslashes

How to serialize JSON to string without escape characters in .NET Core?

The escape characters aren't there. If we can reasonably assume that your sensorData is something like:

class SensorData
{
public int Id { get; set; }
public DateTime TimeStamp { get; set; }
public double Value { get; set; }
}

then if I do:

var sensorData = new SensorData {
Id = 100201,
TimeStamp = DateTime.Now,
Value = 12.3,
};
string s = JsonConvert.SerializeObject(sensorData);
Console.WriteLine(s);

the output is:

{"Id":100201,"TimeStamp":"2018-02-08T10:30:40.2290218+00:00","Value":12.3}

The escape characters are an IDE feature, not part of the JSON. The IDE shows you strings as you would need to copy and paste them in your target language; so: if the value is the JSON posted above, then the IDE will show it as a C# literal:

"{\"Id\":100201, ...snip... , \"Value\":12.3}"

But: that isn't the contents of the string.

Json.NET adding backslash while returning json serialized string

No. it doesn't

class Program
{
class Book
{
public int ID;
public string BookName;
}

static void Main()
{
var books = new List<Book> { new Book { ID = 1, BookName = "A" }, new Book { ID = 2, BookName = "B" } };

var x = from d in books
select new
{
ID = d.ID,
BookName = d.BookName
};

string str = JsonConvert.SerializeObject(x.ToList());
Console.WriteLine(str);
}
}

There could be two problems:

A) You are looking at the result from the debugger. To check for this, Put the JsonConvert in a temporary variable (like I did) and look at it with the debugger. Click on the arrow right of the hourglass and select Text Visualizer.

or

B) The calling method is transforming the object again to Json, so escaping everything.

Json.Net unexpected characters ("\") when serializing my entities

I found the reason why I had escape characters in my string ("\"). After serializing my objects, I am returning the JSON string to the client app through a WCF. Apparently, WCF is automatically adding these characters to the string before sending it to the network. It is a default behaviour and is apparently mandatory.

As I didn't want these escape characters, the workaround is to change the return type of the service to Stream and so, returning your JSON string inside a memory stream. It works perfectly and is quite fast.

C# Doesn't Remove Backslashes While Writing JSON Data Into File

As has been noted in comments, the problem is that you're converting an object into a JSON string here:

string propertyStr = JsonConvert.SerializeObject(propertiesArray);

And then you're setting that string (which includes double quotes etc) as a JSON property here:

result["properties"] = propertyStr;

I believe you don't want it as a string property, but just as the object. So instead, you want the value of the property to be the JToken itself. So I'd expect this to work:

result["properties"] = JToken.FromObject(propertiesArray);


Related Topics



Leave a reply



Submit