Search code examples
c#.netjsonserializationjavascriptserializer

How do I escape HTML elements in C# .NET using JavaScriptSerializer?


Consider this code:

// Dictionary created.

Dictionary<string, string> objDic = new Dictionary<string, string>();
string test = "<>";
objDic.Add("html", test);

var Json = new JavaScriptSerializer();

string response = Json.Serialize(return_obj);

Output response:

[{"html":"\u003c\u003e"}]

Expected response:

[{"html":"<>"}]

How do I get the expected response?


Solution

  • Then you can use Json.NET:

    var str = JsonConvert.SerializeObject(new {html="<>"}) //returns {"html":"<>"}