C# Json.NET WCF Rest DateTime Format

当调用 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);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: .Net WCF面试题是关于.NET WCF(Windows Communication Foundation)的面试题。WCF是一种用于构建分布式应用程序的微软技术。它提供了一种统一的编程模型,使开发人员能够使用不同的传输协议和编码方式来进行通信。WCF面试题可能涉及到WCF的基本概念、架构、服务契约、终结点等方面的内容。在回答这些问题时,可以引用.NET Framework的相关知识,如.NET Framework是微软的一个应用程序开发框架,它包含了许多不同的组件,如ASP.NET、ADO.NET、Windows Forms等。此外,还可以引用HttpHandler的概念,它是ASP.NET中用于处理传入Http请求的低级API。List<T>和Dictionary<K,V>是.NET Framework中常用的集合类型,它们有不同的特点和用途。List<T>是一个动态数组,可以按索引访问元素,而Dictionary<K,V>是一个键值对集合,可以通过键来访问值。它们的区别和优势可以根据具体的需求来进行比较和说明。 #### 引用[.reference_title] - *1* *3* [互联网面试——.Net 面试题](https://blog.csdn.net/Cool2Feel/article/details/118000408)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [.NET软件开发工程师面试题(上)](https://blog.csdn.net/m0_56366948/article/details/127974031)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值