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
序列化之后返回的格式如我们指定的格式了~
Newtonsoft.Json 指定某个属性使用特定的时间格式的更多相关文章
- Json.Net 使用属性定义日期的序列化格式
如果一个实体类里所有的时间即DateTime类型的字段,都处理成统一格式的话,可以使用如下方式: IsoDateTimeConverter timeFormat = new IsoDateTimeCo ...
- Jackson将对象转换为json字符串时,设置默认的时间格式
maven需要的依赖: <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifac ...
- EF 实体+ Newtonsoft.Json 输出JSON 时动态忽略属性的解决方法
最近的项目采用的是 ASP.NET mvc 4.0 + entity framework 5.0 ,后台以JSON形式抛出数据是借助于Newtonsoft.Json , 要想忽略的属性前面添加特性 ...
- C# Newtonsoft.Json JObject移除属性,在序列化时忽略
原文 C# Newtonsoft.Json JObject移除属性,在序列化时忽略 一.针对 单个 对象移除属性,序列化时忽略处理 JObject实例的 Remove() 方法,可以在 指定序列化时移 ...
- 分享基于.NET动态编译&Newtonsoft.Json封装实现JSON转换器(JsonConverter)原理及JSON操作技巧
看文章标题就知道,本文的主题就是关于JSON,JSON转换器(JsonConverter)具有将C#定义的类源代码直接转换成对应的JSON字符串,以及将JSON字符串转换成对应的C#定义的类源代码,而 ...
- MVC日期格式化,后台使用Newtonsoft.Json序列化日期,前端使用”f”格式化日期
MVC控制器中,经常使用Newtonsoft.Json把对象序列化成json字符串传递到前端视图.当对象中有DateTime类型的属性时,前后台如何处理才能把DateTime类型转换成想要的格式呢? ...
- 时间格式的时间 转json的时候变成正常的string时间20170519
public class JsonDateValueProcessor implements JsonValueProcessor { private String format ="yyy ...
- Newtonsoft.Json 序列化 排除指定字段或只序列化指定字段
using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System; using System.Collections.G ...
- Newtonsoft.Json动态过滤属性
Newtonsoft.Json动态过滤属性 接口写的多了,会发现很多的问题.同一个dto,不同的action返回的字段个数不一样.往往开发人员因为懒或者各种原因一股脑的全返回,会浪费很多流量且用户体验 ...
随机推荐
- Java之Iterator接口(遍历单列集合的迭代器)
Iterator接口概述 在程序开发中,经常需要遍历集合中的所有元素.针对这种需求,JDK专门提供了一个接口java.util.Iterator . Iterator 接口也是Java集合中的一员,但 ...
- PostgreSQL数据库一些tricks
PostgreSQL自带Pgadmin客户端,可用于访问本地和远程PG库,一些tricks如下: 1.联合查询 SELECT * FROM table1 INNER JOIN table2 ON ta ...
- Linux习题小结
1.输出当前下的目录.ls -l 长格式输出. (1)使用grep 因为第一个 ls -l 的第一个标识代表的是文件类型,所以使用 grep 过滤以 d 开头的行,输出的就只是目录了. 正则表达式 g ...
- django.db.utils.OperationalError: (1093, "You can't specify target table 'xxx' for update in FROM clause")
这个错误的意思是,不能在update某张表的where条件中,再次select这张表的某些值作为筛选条件,比如: update message set content = "hello&qu ...
- MVC邮箱验证
post请求 [HttpPost] public void Email(Models.Email m,string Txt) { if (Txt!= Session["yzm"]. ...
- Java生鲜电商平台-电商虚拟币的充值与消费思考
Java生鲜电商平台-电商虚拟币的充值与消费思考 项目背景 最近由于项目业务原因,需要为系统设计虚拟币的充值及消费功能.公司内已经有成熟的支付网关服务,所以重点变成了如何设计项目内虚拟币的充值流程,让 ...
- javaWeb核心技术第十四篇之easyui
网站是分为网站的前台和网站的后台. 前台--给用户看的 例如:商城 后台--给管理员看的 例如:商城后台 目的:用来添加维护数据 BootStrap:jsp 页面显示,效果好,美观,适合作为用户界面. ...
- 机器学习pipeline总结
# -*- coding: utf-8 -*- """scikit-learn introduction Automatically generated by Colab ...
- 在vue中添加ico图标
准备:添加 ico图标在与index.html同级的目录 第一种方法: 在index.html中引入: <link rel="shortcuticon" type=" ...
- apache部分报错解决方法
AH00558: 进入apache文件夹下的conf文件夹,打开httpd.conf文件,用ctrl+F找到ServerName,如下图 在下面加上一句: ServerName domain_name ...