C#中使用Newtonsoft.Json序列化和反序列化自定义类对象

在C#中序列化和反序列化自定义的类对象是比较容易的,比如像下面的一个Customer类,

private class Customer
{
    public string CustomerName { get; set; }
    public string Email { get; set; }
    public int Age { get; set; }
    public decimal TotalSales { get; set; }
    public DateTime FinalPurchaseDate { get; set; }
}

在Windows10系统中使用VS2017创建一个基于C#控制台的.Net控制台应用程序JsonExample01
然后使用NuGet安装Newtonsoft.Json的包,

Newtonsoft.Json包

下面是相关的C#测试代码:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JsonExample01
{
    class Program
    {
        static void Main(string[] args)
        {
            var customers = new List
            {
                new Customer
                {
                    CustomerName = "John",
                    Age = 33,
                    Email = "[email protected]",
                    TotalSales = 4000,
                    FinalPurchaseDate = new DateTime(2021, 3, 29)
                },
                new Customer
                {
                    CustomerName = "George",
                    Age = 32,
                    Email = "[email protected]",
                    TotalSales = 6000,
                    FinalPurchaseDate = new DateTime(2021, 3, 29)
                },
                new Customer
                {
                    CustomerName = "Peter",
                    Age = 28,
                    Email = "[email protected]",
                    TotalSales = 2000,
                    FinalPurchaseDate = new DateTime(2021, 1, 10)
                }
            };

            var customer = new Customer
            {
                CustomerName = "Peter",
                Age = 28,
                Email = "[email protected]",
                TotalSales = 2000,
                FinalPurchaseDate = new DateTime(2021, 1, 10)
            };

            var customerJson1 = JsonConvert.SerializeObject(customer);
            Console.WriteLine(customerJson1);

            var customerListJson2 = JsonConvert.SerializeObject(customers);
            Console.WriteLine(customerListJson2);

            //var strJson1 = "{\"CustomerName\":\"Peter\",\"Email\":\"[email protected]\",\"Age\":28,\"TotalSales\":2000.0,\"FinalPurchaseDate\":\"2021-01-10T00:00:00\"}";
            //var customer2 = JsonConvert.DeserializeObject(strJson1);

            //var strJson = "[{\"CustomerName\":\"John\",\"Email\":\"[email protected]\",\"Age\":33,\"TotalSales\":4000.0,\"FinalPurchaseDate\":\"2021-03-29T00:00:00\"},{\"CustomerName\":\"George\",\"Email\":\"[email protected]\",\"Age\":32,\"TotalSales\":6000.0,\"FinalPurchaseDate\":\"2021-03-29T00:00:00\"},{\"CustomerName\":\"Peter\",\"Email\":\"[email protected]\",\"Age\":28,\"TotalSales\":2000.0,\"FinalPurchaseDate\":\"2021-01-10T00:00:00\"}]";
            //var customerList2 = JsonConvert.DeserializeObject>(strJson);

            var strJson1 = "{\"customer_name\":\"Peter\",\"email\":\"[email protected]\",\"age\":28,\"total_sales\":2000.0,\"final_purchase_date\":\"2021-01-10T00:00:00\"}";
            var customer2 = JsonConvert.DeserializeObject(strJson1);

            var strJson = "[{\"customer_name\":\"John\",\"email\":\"[email protected]\",\"age\":33,\"total_sales\":4000.0,\"final_purchase_date\":\"2021-03-29T00:00:00\"},{\"customer_name\":\"George\",\"email\":\"[email protected]\",\"age\":32,\"total_sales\":6000.0,\"final_purchase_date\":\"2021-03-29T00:00:00\"},{\"customer_name\":\"Peter\",\"email\":\"[email protected]\",\"age\":28,\"total_sales\":2000.0,\"final_purchase_date\":\"2021-01-10T00:00:00\"}]";
            var customerList2 = JsonConvert.DeserializeObject>(strJson);

            Console.ReadKey();
        }

        [Serializable]
        private class Customer
        {
            [JsonProperty("customer_name")]
            public string CustomerName { get; set; }
            [JsonProperty("email")]
            public string Email { get; set; }
            [JsonProperty("age")]
            public int Age { get; set; }
            [JsonProperty("total_sales")]
            public decimal TotalSales { get; set; }
            [JsonProperty("final_purchase_date")]
            public DateTime FinalPurchaseDate { get; set; }
        }
    }

   
}

运行结果如下图所示:


运行结果

你可能感兴趣的:(C#中使用Newtonsoft.Json序列化和反序列化自定义类对象)