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


    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; }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在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<Customer>
    {
    new Customer
    {
    CustomerName = "John",
    Age = 33,
    Email = "john@gmail.com",
    TotalSales = 4000,
    FinalPurchaseDate = new DateTime(2021, 3, 29)
    },
    new Customer
    {
    CustomerName = "George",
    Age = 32,
    Email = "george@gmail.com",
    TotalSales = 6000,
    FinalPurchaseDate = new DateTime(2021, 3, 29)
    },
    new Customer
    {
    CustomerName = "Peter",
    Age = 28,
    Email = "john@gmail.com",
    TotalSales = 2000,
    FinalPurchaseDate = new DateTime(2021, 1, 10)
    }
    };

            var customer = new Customer
            {
                CustomerName = "Peter",
                Age = 28,
                Email = "john@gmail.com",
                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\":\"john@gmail.com\",\"Age\":28,\"TotalSales\":2000.0,\"FinalPurchaseDate\":\"2021-01-10T00:00:00\"}";
            //var customer2 = JsonConvert.DeserializeObject&lt;Customer&gt;(strJson1);
    
            //var strJson = "[{\"CustomerName\":\"John\",\"Email\":\"john@gmail.com\",\"Age\":33,\"TotalSales\":4000.0,\"FinalPurchaseDate\":\"2021-03-29T00:00:00\"},{\"CustomerName\":\"George\",\"Email\":\"george@gmail.com\",\"Age\":32,\"TotalSales\":6000.0,\"FinalPurchaseDate\":\"2021-03-29T00:00:00\"},{\"CustomerName\":\"Peter\",\"Email\":\"john@gmail.com\",\"Age\":28,\"TotalSales\":2000.0,\"FinalPurchaseDate\":\"2021-01-10T00:00:00\"}]";
            //var customerList2 = JsonConvert.DeserializeObject&lt;List&lt;Customer&gt;&gt;(strJson);
    
            var strJson1 = "{\"customer_name\":\"Peter\",\"email\":\"john@gmail.com\",\"age\":28,\"total_sales\":2000.0,\"final_purchase_date\":\"2021-01-10T00:00:00\"}";
            var customer2 = JsonConvert.DeserializeObject&lt;Customer&gt;(strJson1);
    
            var strJson = "[{\"customer_name\":\"John\",\"email\":\"john@gmail.com\",\"age\":33,\"total_sales\":4000.0,\"final_purchase_date\":\"2021-03-29T00:00:00\"},{\"customer_name\":\"George\",\"email\":\"george@gmail.com\",\"age\":32,\"total_sales\":6000.0,\"final_purchase_date\":\"2021-03-29T00:00:00\"},{\"customer_name\":\"Peter\",\"email\":\"john@gmail.com\",\"age\":28,\"total_sales\":2000.0,\"final_purchase_date\":\"2021-01-10T00:00:00\"}]";
            var customerList2 = JsonConvert.DeserializeObject&lt;List&lt;Customer&gt;&gt;(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; }
        }
    }
    

    }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90

    运行结果如下图所示:
    程序运行结果

    https://blog.csdn.net/ccf19881030/article/details/116374465
  • 相关阅读:
    【ABAP系列】SAP ABAP OOALV 动态设置单元格可否编辑
    【HANA系列】SAP HANA使用XS和HTTP创建proxy
    【HANA系列】SAP HANA SLT在表中隐藏字段并传入HANA的方法
    【BW系列】SAP 讲讲BW/4 HANA和BW on HANA的区别
    【HANA系列】SAP HANA 2.0 SPS00 SDA(Smart Data Access)连接Hadoop
    【BW系列】SAP BW on HANA 迁移问题
    【HANA系列】SAP HANA行列转换
    【HANA系列】SAP ECLIPSE中创建ABAP项目失败原因解析
    开放-封闭原则
    单一职责原则
  • 原文地址:https://www.cnblogs.com/sunny3158/p/16785688.html
Copyright © 2020-2023  润新知