在使用Json.NET来序列化和反序列化对象时,我们可以在类或者属性上声明使用什么命名规则,Json.NET目前支持三种命名规则:
- 帕斯卡命名法(默认):DefaultNamingStrategy
- 驼峰命名法:CamelCaseNamingStrategy
- 下划线命名法:SnakeCaseNamingStrategy
这三个类可以通过JsonObjectAttribute声明在类上,也可以通过JsonPropertyAttribute声明在属性或者字段上,如下代码所示:
using Newtonsoft.Json.Serialization; using Newtonsoft.Json; namespace Net8JsonNameDemo { //给整个类声明使用驼峰命名法,来序列化和反序列化Json [JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))] class People { public required string Name { get; set; } public int? Age { get; set; } public string? Description { get; set; } //给某个属性声明使用下划线命名法,来序列化和反序列化Json [JsonProperty(NamingStrategyType = typeof(SnakeCaseNamingStrategy))] public decimal? BaseSalary { get; set; } } //给整个类声明使用帕斯卡命名法(默认就是帕斯卡命名法),来序列化和反序列化Json [JsonObject(NamingStrategyType = typeof(DefaultNamingStrategy))] class Person { public required string Name { get; set; } //给某个属性声明使用驼峰命名法,来序列化和反序列化Json [JsonProperty(NamingStrategyType = typeof(CamelCaseNamingStrategy))] public int? Age { get; set; } public string? Description { get; set; } //给某个属性声明使用驼峰命名法,来序列化和反序列化Json [JsonProperty(NamingStrategyType = typeof(CamelCaseNamingStrategy))] public decimal? BaseSalary { get; set; } } internal class Program { static void Main(string[] args) { People people = new People() { Name = "王大锤, Jack Wang", Age = 16, BaseSalary = 1000, Description = "This is a demo text,这是示例文本" }; string json = JsonConvert.SerializeObject(people, Formatting.Indented); Console.WriteLine(json); People? newPeople = JsonConvert.DeserializeObject<People>(json); Console.WriteLine("newPeople Name={0}; Age={1}; BaseSalary={2}; Description={3}", newPeople?.Name, newPeople?.Age, newPeople?.BaseSalary?.ToString(), newPeople?.Description?.ToString()); Person person = new Person() { Name = "张小明, James Zhang", Age = 25, BaseSalary = 2000, Description = "This is a demo text2,这是示例文本2" }; Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); json = JsonConvert.SerializeObject(person, Formatting.Indented); Console.WriteLine(json); Person? newPerson = JsonConvert.DeserializeObject<Person>(json); Console.WriteLine("newPerson Name={0}; Age={1}; BaseSalary={2}; Description={3}", newPerson?.Name, newPerson?.Age, newPerson?.BaseSalary?.ToString(), newPerson?.Description?.ToString()); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("Press any key to end..."); Console.ReadLine(); } } }
运行上面的代码,结果如下所示:
{ "name": "王大锤, Jack Wang", "age": 16, "description": "This is a demo text,这是示例文本", "base_salary": 1000.0 } newPeople Name=王大锤, Jack Wang; Age=16; BaseSalary=1000.0; Description=This is a demo text,这是示例文本 { "Name": "张小明, James Zhang", "age": 25, "Description": "This is a demo text2,这是示例文本2", "baseSalary": 2000.0 } newPerson Name=张小明, James Zhang; Age=25; BaseSalary=2000.0; Description=This is a demo text2,这是示例文本2 Press any key to end...
可以参考下面的官方文档,有更多方法来声明Json.NET的命名规则:
JsonObjectAttribute NamingStrategy setting
Configure NamingStrategy dictionary serialization
Configure NamingStrategy property name serialization
标签:Console,Description,Json,WriteLine,NET,序列化,Name From: https://www.cnblogs.com/OpenCoder/p/18688648