使用Json.NET序列化和反序列化对象时,声明命名规则
在使用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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
2019-01-23 ASP.NET Core 使用外部登陆提供程序登陆的流程,以及身份认证的流程 (转载)
2019-01-23 SQL Server中比较末尾带有空格的字符串遇到的坑 (转载)
2019-01-23 ASP.NET Core如何设置请求超时时间
2019-01-23 ADO.NET的Connection Timeout和Command Timeout (转载)
2019-01-23 jquery ajax超时设置(转载)
2016-01-23 SQL SERVER: 合并相关操作(Union,Except,Intersect) - 转载