rnli rnli - 1 month ago 24
ASP.NET (C#) Question

Serialize string to JSON without \0027

A fairly straightforward question. I have the following code

var json = new JavaScriptSerializer();
var test = json.Serialize("'");


Now, Visual Studio local variable watch shows that
test
contains a string of value
"\"\\u0027\""
. Is it possible, using build in ASP.NET to make
Serialize();
return serialized string without
\0027
format?

The desired result for
test
would be
"\"\'\""


Thanks!

Answer

As the commenters mentioned, this really shouldn't matter because \u0027 literally represents a single quote in Javascript.

However, if this bothers you, you may want to try using JSON.NET, which leaves single-quotes in encoded strings as they are.