添加引用:

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

相关文章:

  • 2022-12-23
  • 2021-05-30
  • 2022-12-23
  • 2021-06-03
猜你喜欢
  • 2022-12-23
  • 2021-05-28
  • 2022-12-23
  • 2021-10-20
  • 2021-07-29
  • 2021-09-22
相关资源
相似解决方案