当调用 Restful 的WCF 服务时,如果参数是Json格式,参数中有 DateTime 类型时会提示 时间类型的格式 序列化错误:
Take a look at the following example code, I would like the output to be a WCF date format "/Date(1237951967000)/" or the time zone variant.
class Program
{
public class Test
{
public DateTime Date { get; set; }
}
static void Main(string[] args)
{
var test = new Test
{
Date = DateTime.Now
};
var json = JsonConvert.SerializeObject(test);
Console.WriteLine(json);
}
}
Here is the output:
{"Date":"2013-05-09T11:17:38.7990259-07:00"}
How can I adjust the above code to give the desired format?
{"Date":"\/Date(1237951967000)\/"}
需要在调用时服务序列化参数对象时,作如下设置:
var settings = new JsonSerializerSettings() {DateFormatHandling= DateFormatHandling.MicrosoftDateFormat};
var json = JsonConvert.SerializeObject(test, settings);