您的位置:首页 > Web前端 > JavaScript

datetime json 序列化时丢掉时区

2016-05-05 13:00 579 查看
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;
}
}


修改以后生成的示例:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息