首页 > Windows开发 > 详细

MVC web api 返回JSON的几种方式,Newtonsoft.Json序列化日期时间去T的几种方式

时间:2019-06-01 12:04:44      阅读:158      评论:0      收藏:0      [点我收藏+]
MVC web api 返回JSON的几种方式,Newtonsoft.Json序列化日期时间去T的几种方式

2015-01-18 00:11 https://www.muhanxue.com/essays/2015/01/8623699.html

MVC web api 返回JSON的几种方式

1、在 WebApiConfig的 Register中加入以下代码

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

2、在 WebApiConfig的 Register中加入以下代码

config.Formatters.Remove(config.Formatters.XmlFormatter);

3、在 WebApiApplication的 Application_Start中加入以下代码

GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();

在MVC中全局去除时间格式中带T的问题。

1、MVC中默认使用Newtonsoft.Json序列化的,所以在 WebApiConfig的 Register中加入以下代码即可

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new IsoDateTimeConverter
{
    DateTimeFormat = "yyyy‘-‘MM‘-‘dd‘ ‘HH‘:‘mm‘:‘ss"
});

2、在 webservice中去除时间带T的问题。

IsoDateTimeConverter timejson = new IsoDateTimeConverter
{
    DateTimeFormat = "yyyy‘-‘MM‘-‘dd‘ ‘HH‘:‘mm‘:‘ss"
};
//在序列化的时候传入timejson对象
return JsonConvert.SerializeObject(object, timejson); // object是需要序列化的对象

MVC web api 返回JSON的几种方式,Newtonsoft.Json序列化日期时间去T的几种方式

原文:https://www.cnblogs.com/Jeely/p/10959008.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!