.NET环境下生成JSON的类库 - JSON.NET

xiaoxiao2022-11-25  37

网站以后的例子都会以asp.net为主,生成JSON数据也将会用JSON.NET这一个类库。废话少说,下面我们一起来看看它的例子:

示例代码

Product product = new Product();   product.Name = "Apple";   product.Expiry = new DateTime(2008, 12, 28);   product.Price = 3.99M;   product.Sizes = new string[] { "Small""Medium""Large" };       string json = JavaScriptConvert.SerializeObject(product);       Product deserializedProduct = JavaScriptConvert.DeserializeObject<Product>(json);   Product product = new Product(); product.Name = "Apple"; product.Expiry = new DateTime(2008, 12, 28); product.Price = 3.99M; product.Sizes = new string[] { "Small", "Medium", "Large" }; string json = JavaScriptConvert.SerializeObject(product); Product deserializedProduct = JavaScriptConvert.DeserializeObject<Product>(json);

 

生成的JSON结果如下:

 

{     "Name""Apple",     "Expiry"new Date(1230422400000),     "Price": 3.99,     "Sizes": [       "Small",       "Medium",       "Large"    ]   }   { "Name": "Apple", "Expiry": new Date(1230422400000), "Price": 3.99, "Sizes": [ "Small", "Medium", "Large" ] } 下载地址:(最新版本3.0需要.net framework 3.5 ,如果你使用.net framework 2.0 那么请下载1.31 )

 

http://www.codeplex.com/json/Release/ProjectReleases.aspx

 

http://code.google.com/p/jsonsharp/

 

http://json.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=21341

相关资源:Newtonsoft.Json 类库 最新.NET4.5版本
转载请注明原文地址: https://www.6miu.com/read-4979065.html

最新回复(0)