-
C#教程之使用Newtonsoft序列化
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
Newtonsoft是我们开发过程中经常用到的一个第三方类库,主要用于对象的序列化和反序列化。
命名方式
默认情况下序列化后的json字符串会以类名、属性名作为键来命名。问题在于C#的命名规范中类名、属性名都是以PascalCase方式来命名的,而在前端中一般都是以CamelCase方式来命名的,所以我们可以通过Newtonsoft提供的一些方法来满足我们所需的效果,直接看示例:
public class Book { public string BookName { get; set; } public decimal BookPrice { get; set; } public string AuthorName { get; set; } public int AuthorAge { get; set; } public string AuthorCountry { get; set; } }
Book book = new Book { BookName = "The Gathering Storm", BookPrice = 16.19m, AuthorName = "Brandon Sanderson", AuthorAge = 34, AuthorCountry = "United States of America" }; string json1 = JsonConvert.SerializeObject(book, new JsonSerializerSettings { ContractResolver = new DefaultContractResolver() }); //首字母大写,PascalCase方式 //{ // "BookName": "The Gathering Storm", // "BookPrice": 16.19, // "AuthorName": "Brandon Sanderson", // "AuthorAge": 34, // "AuthorCountry": "United States of America" //} string json2 = JsonConvert.SerializeObject(book, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); //首字母小写,CamelCase方式 //{ // "bookName": "The Gathering Storm", // "bookPrice": 16.19, // "authorName": "Brandon Sanderson", // "authorAge": 34, // "authorCountry": "United States of America" //}
栏目列表
最新更新
C#中获取指定路径下指定后缀名的所有文
使用Newtonsoft序列化
C#函数(构造函数)的重载
Geometry 判断几何是否被另一个几何/线段分
C# 矢量图EMF 总结
C#泛型自己的理解和总结
数据结构之Array、ArrayList、List、LinkedLis
13. 罗马数字转整数(C#)
C#中在定义事件委托时怎样跨窗体传递参
Winform中设置ZedGraph的X轴的刻度根据曲线获
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能
将Session值储存于SQL Server中
使用SQL Server数据库嵌套子查询的方法
SQL概述