Newtonsoft.Json 指定某个属性使用特定的时间格式
程序员文章站
2022-09-27 22:23:37
`newtonsoft.json` 是 .NET 下最受欢迎 JSON 操作库,原为 `JSON.Net` 后改名为 `Newtonsoft.Json`,之前一直推荐大家使用,除了性能好之外,主要是功能丰富,基本满足所有的可能用到的场景(不区分小写,现在还不行,,)。 遇到这样一个需求,全局使用一... ......
newtonsoft.json
指定某个属性使用特定的时间格式
intro
newtonsoft.json
是 .net 下最受欢迎 json 操作库,原为 json.net
后改名为 newtonsoft.json
,之前一直推荐大家使用,除了性能好之外,主要是功能丰富,基本满足所有的可能用到的场景(不区分小写,现在还不行,,)。
遇到这样一个需求,全局使用一种时间格式,某些属性使用特殊的时间格式,这里以一个日期为例
solution
解决办法:自定义一个 converter,针对某一个属性使用,datetimeformatconverter源码:
using newtonsoft.json.converters; namespace weihanli.common.json { public class datetimeformatconverter : isodatetimeconverter { public datetimeformatconverter(string format) { datetimeformat = format; } } }
在需要设置格式的属性上设置 converter https://github.com/weihanli/activityreservation/blob/dev/activityreservation.helper/viewmodels/reservationviewmodel.cs#l8
[display(name = "预约日期")] [jsonconverter(typeof(datetimeformatconverter), "yyyy-mm-dd")] public datetime reservationfordate { get; set; }
请求 api 地址 https://reservation.weihanli.xyz/api/reservation?pagenumber=1&pagesize=5,返回的数据如下所示:
{ "data": [ { "reservationfordate": "2019-06-10", "reservationfortime": "08:00~09:50", "reservationpersonphone": "123****0112", "reservationpersonname": "儿**", "reservationunit": "51", "reservationplacename": "多媒体工作室", "reservationactivitycontent": "62", "reservationid": "f7ab9128-0977-4fd8-9b1a-92648228b397", "reservationtime": "2019-06-09 05:19:11", "reservationstatus": 1 }, { "reservationfordate": "2019-06-12", "reservationfortime": "10:00-12:00", "reservationpersonphone": "133****3541", "reservationpersonname": "试**", "reservationunit": "ss", "reservationplacename": "多媒体工作室", "reservationactivitycontent": "ss", "reservationid": "6c145aea-dc14-4ed9-a47f-48c0b79f7601", "reservationtime": "2019-06-11 12:45:14", "reservationstatus": 0 }, { "reservationfordate": "2019-06-17", "reservationfortime": "14:00-16:00", "reservationpersonphone": "138****3883", "reservationpersonname": "大**", "reservationunit": "1", "reservationplacename": "多媒体工作室", "reservationactivitycontent": "1", "reservationid": "cebea7bf-44b1-4565-8cdd-78b6156c5f4d", "reservationtime": "2019-06-10 02:52:18", "reservationstatus": 1 }, { "reservationfordate": "2019-06-17", "reservationfortime": "08:00-10:00", "reservationpersonphone": "132****4545", "reservationpersonname": "冷**", "reservationunit": "技术部", "reservationplacename": "多媒体工作室", "reservationactivitycontent": "技术部培训", "reservationid": "07f6f8fd-f232-478e-9a94-de0f5fa9b4e9", "reservationtime": "2019-06-10 01:44:52", "reservationstatus": 2 }, { "reservationfordate": "2019-06-22", "reservationfortime": "10:00~11:50", "reservationpersonphone": "132****3333", "reservationpersonname": "测**", "reservationunit": "测试", "reservationplacename": "多媒体工作室", "reservationactivitycontent": "测试", "reservationid": "27d0fb7a-ce14-4958-8636-dd10e5526083", "reservationtime": "2019-06-18 10:57:06", "reservationstatus": 1 } ], "pagenumber": 1, "pagesize": 5, "totalcount": 18, "pagecount": 4, "count": 5 }
可以看到 reservationfordate
序列化之后返回的格式如我们指定的格式了~
上一篇: C#中await/async闲说
下一篇: 多线程学习(一)