I'm having some trouble serializing an object to a JSON string using System.Web.Script.Serialization.JavaScriptSerializer. Whenever I try to do it, my strings are automatically html encoded. Is there a way to prevent this from happening? I'd really like to avoid using an external library if possible (code is for .NET 4). Here's my code:
class Program
{
static void Main(string[] args)
{
string myHtml = "<div class=\"blueBackground\">This is a really cool div:)</div>";
int someOtherValue = 5;
var jsonSerializer = new JavaScriptSerializer();
string jsonObj = jsonSerializer.Serialize(new MyClass
{
StringProperty = myHtml,
IntProperty = someOtherValue
});
Console.WriteLine(jsonObj);
Console.ReadLine();
}
class MyClass
{
public string StringProperty { get; set; }
public int IntProperty { get; set; }
}
}
It outputs the string
{"StringProperty":"\u003cdiv class=\"blueBackground\"\u003eThis is a really cool div:)\u003c/div\u003e","IntProperty":5}
Thanks!