1
我在使用System.Web.Script.Serialization.JavaScriptSerializer將對象序列化爲JSON字符串時遇到了一些麻煩。每當我嘗試這樣做時,我的字符串都會自動進行html編碼。有沒有辦法來防止這種情況發生?如果可能,我真的想避免使用外部庫(代碼是.NET 4)。這裏是我的代碼:使用JavaScriptSerializer序列化爲JSON的麻煩
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; }
}
}
它輸出字符串
{ 「StringProperty」: 「\ u003cdiv類= \」 blueBackground \「\ u003eThis是 真的很酷格:)\ u003c/DIV \ u003e「,」IntProperty「:5}
謝謝!
哇,謝謝Darin :)我甚至都不知道有這樣的事情。 – Andrew