datetime json 序列化时丢掉时区

来源:互联网 发布:08总决赛皮尔斯数据 编辑:程序博客网 时间:2024/04/19 12:22

asp.net mvc web api test client 是个好东西,能够直接测试api调用。

但有一点是,生成datetime类型的测试数据时,是带有时区的,导致在调用的时候,反序列化失败。不得不手动修改一下时间的格式。

如下图:


那我们就手动修改一下代码,使其序列化时放弃时区吧

代码修改对比:


位置:

file: $\Areas\HelpPage\SampleGeneration\HelpPageSampleGenerator.cs 

class: HelpPageSampleGenerator 

Method: private static string TryFormatJson(string str)

LN: 380

        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Handling the failure by returning the original string.")]        private static string TryFormatJson(string str)        {            try            {                object parsedJson = JsonConvert.DeserializeObject(str);                Newtonsoft.Json.Converters.IsoDateTimeConverter timeFormat = new Newtonsoft.Json.Converters.IsoDateTimeConverter();                timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";                return JsonConvert.SerializeObject(parsedJson, Formatting.Indented, timeFormat);            }            catch            {                // can't parse JSON, return the original string                return str;            }        }

修改以后生成的示例:




0 0
原创粉丝点击