Newtonsoft.Json(Json.net)的基本用法

添加引用:

使用NuGet,命令:install-package Newtonsoft.Json

 

实体类:

    public class Book
    {
        public string BookID { get; set; }
        public DateTime PublishDate { get; set; }
        public decimal Price { get; set; }

        public override string ToString()
        {
            return "ID:" + BookID + "; Date:" + PublishDate.ToShortDateString() + "; Price" + Price.ToString("n");
        }
    }

 

序列化和反序列化:

            Book bk = new Book() { BookID = "12111", PublishDate = DateTime.Parse("2012-2-1 22:12:11"), Price=433.12M};
            Console.WriteLine(JsonConvert.SerializeObject(bk));

            string jsonBook = "{'BookID':'123', 'PublishDate':'2011-1-2', 'Price':23.5}";
            Book bk1 = JsonConvert.DeserializeObject<Book>(jsonBook);
            Console.WriteLine(bk1.ToString());
posted @ 2012-05-28 10:22  拓荒者FF  阅读(37475)  评论(1编辑  收藏  举报