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

前端之家收集整理的这篇文章主要介绍了Newtonsoft.Json(Json.net)的基本用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


添加引用:

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

实体类:

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

        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());

猜你在找的Json相关文章