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
的包,
下面是相关的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 = "[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<Customer>(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<List<Customer>>(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<Customer>(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<List<Customer>>(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
运行结果如下图所示: