Newtonsoft.Json 对象序列化
时间:2011-02-20 来源:程序员蛋子
在asp.net网站中添加一个linq to sql 类,创建数据类Customers(数据库 NorthWind)
DataClassesDataContext dd = new DataClassesDataContext();
//通过CustomerID获取一个Customers类实例
Customers cus = dd.Customers.Single(c => c.CustomerID == "11111");
//目标对象序列化为json字符串
string Str = Newtonsoft.Json.JsonConvert.SerializeObject(cus);
Response.Write(Str + "<br/>");
string Str1=@"{""CustomerID"":""11111"",""CompanyName"":""11111"",""ContactName"":""111111111"",""ContactTitle"":""1111111"",""Address"":""1111111"",""City"":null,""Region"":null,""PostalCode"":null,""Country"":null,""Phone"":null,""Fax"":null} ";
//json字符串反序列化为对象
Customers cus1 = JsonConvert.DeserializeObject<Customers>(Str1);
Response.Write(cus1.CustomerID + "<br/>");
Response.Write(cus1.CompanyName + "<br/>");
Response.Write(cus1.Address + "<br/>");
注:在vs2010中序列化会报错,还有那位大大有更多的资料希望与小的分享。