
Newtonsoft.Json使用总结
介绍
Json.NET is a popular high-performance JSON framework for .NET
使用方法
可以在官网或github仓库查看详细文档。在ide中使用时,无需下载代码,在nuget包管理器中搜索即可,或直接using Newtonsoft.Json;
,再使用Alt + Enter智能引用
序列化
简单用法:
1 | JsonConvert.SerializeObject(object? value); |
可以创建扩展类方便使用:
1 | public static class NewtonsoftJsonExtension |
反序列化
简单用法:
1 | JsonConvert.DeserializeObject<T>(string value); |
可以创建扩展类方便使用:
1 | public static class NewtonsoftJsonExtension |
返回Json时日期格式化
示例
dto类中的属性:
1 | /// <summary> |
日期格式化类:
1 | /// <summary> |
说明
通过继承IsoDateTimeConverter
,然后在ctor
中对DateTimeFormat
属性赋值格式化字符串,之后可以在序列化时需要格式化的DateTime
类型的属性上加上[JsonConverter()]
特性,参数为typeof(继承类)
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 轨 道 兔!
评论