文章出處

  下載地址:Json.NET

  文檔地址:Json.NET Documentation 

基本的序列化與反序列化

1     public class Product
2     {
3         public string Name { get; set; }
4         public DateTime Expiry { get; set; }
5         public string[] Sizes { get; set; }
6     }
1    Product product = new Product();
2    product.Name = "Apple";
3    product.Expiry = new DateTime(2008, 12, 28);
4    product.Sizes = new string[] { "Small" };
5    string json = JsonConvert.SerializeObject(product);
6    Product result = JsonConvert.DeserializeObject<Product>(json);

修改屬性名

1     public class Product
2     {
3         [JsonProperty("Name")]
4         public string ProName { get; set; }
5         [JsonProperty("Expiry")]
6         public DateTime ProExpiry { get; set; }
7         public string[] Sizes { get; set; }
8     }
1     string json = "{\"Name\":\"Apple\",\"Expiry\":\"2008-12-28T00:00:00\",\"Sizes\":[\"Small\"]}";
2     var product = JsonConvert.DeserializeObject<Product>(json);
3     var result = JsonConvert.SerializeObject(product);

Json與XML

   SerializeXNode(DeserializeXNode)和SerializeXmlNode(DeserializeXmlNode)的使用方式基本一樣


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()