Newtonsoft.Json Json.Net的基础用法

主页:http://json.codeplex.com/
NuGet安装:http://nuget.org/packages/Newtonsoft.Json
PM> Install-Package Newtonsoft.Json

类到json字符串的相互转换

public class Product
{
            public string Name{get;set;}
            public DateTime Expiry{get;set;}
            public decimal Price{get;set;}
            public string[] Sizes{get;set;}
}

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 output = JsonConvert.SerializeObject(product);//类转换成Json字符串
//{
//  "Name": "Apple",
//  "Expiry": "\/Date(1230375600000+1300)\/",
//  "Price": 3.99,
//  "Sizes": [
//    "Small",
//    "Medium",
//    "Large"
//  ]
//}
 
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(output);//Json字符串转换成类

 

posted @ 2012-03-02 15:00  虎头  阅读(2442)  评论(0编辑  收藏  举报