下载地址:Json.NET
基本的序列化与反序列化
public class Product { public string Name { get; set; } public DateTime Expiry { get; set; } public string[] Sizes { get; set; } }
Product product = new Product(); product.Name = "Apple"; product.Expiry = , , ); product.Sizes = new string[] { "Small" }; string json = JsonConvert.SerializeObject(product); Product result = JsonConvert.DeserializeObject<Product>(json);
修改属性名
public class Product { [JsonProperty("Name")] public string ProName { get; set; } [JsonProperty("Expiry")] public DateTime ProExpiry { get; set; } public string[] Sizes { get; set; } }
string json = "{\"Name\":\"Apple\",\"Expiry\":\"2008-12-28T00:00:00\",\"Sizes\":[\"Small\"]}"; var product = JsonConvert.DeserializeObject<Product>(json); var result = JsonConvert.SerializeObject(product);
Json与XML
SerializeXNode(DeserializeXNode)和SerializeXmlNode(DeserializeXmlNode)的使用方式基本一样