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