datetime json 序列化时丢掉时区

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;
            }
        }

修改以后生成的示例:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值