电子灵魂

golang,go,C#,JAVA,PYTHON,PHP

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

添加引用:

使用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 on 2015-05-20 19:51  conncent  阅读(466)  评论(0编辑  收藏  举报