首页 > 学院 > 开发设计 > 正文

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

2019-11-17 02:22:37
字体:
来源:转载
供稿:网友
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());

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表