如何使用JavaScriptSerializer在C#.NET中转义HTML元素?

时间:2012-05-11 06:01:29

标签: c# .net json serialization javascriptserializer

考虑以下代码:

// 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);

输出回复:

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

预期回应:

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

如何获得预期的响应?

1 个答案:

答案 0 :(得分:2)

然后您可以使用Json.NET

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