Nimit Parekh Nimit Parekh - 6 months ago 88
JSON Question

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?

L.B L.B
Answer

Then you can use Json.NET:

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