从客户端使用.NET JavaScriptSerializer.Deserialize有日期时间 [英] Using .NET JavaScriptSerializer.Deserialize with DateTime from client

查看:149
本文介绍了从客户端使用.NET JavaScriptSerializer.Deserialize有日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了 JavaScriptSerializer.Deserialize<>()方法将其转换JSON我从客户端接收到一个自定义的C#类。一个是类的属性是的DateTime 。目前,反序列化<>()方法会引发错误,说

I'm using the JavaScriptSerializer.Deserialize<>() method to convert JSON I receive from the client into a custom C# class. One of the properties of that class is a DateTime. Currently the Deserialize<>() method throws an error, saying

(我的日期字符串)不是日期时间的有效值。

"(my date string)" is not a valid value for DateTime.

我已经尝试使用几种不同的格式,包括蜱并通过各种内置的JavaScript的日期()方法产生其他格式发送日期,但他们都没有工作过。

I've tried sending the date using several different formats, including ticks and other formats produced by the various built-in JavaScript Date() methods, but none of them have worked.

究竟是什么格式是反序列化&LT;&GT;()方法,希望以解析成.NET 的DateTime

Exactly what format is the Deserialize<>() method expecting in order to parse it into a .NET DateTime?

推荐答案

您是对的,@friendlycello。 Unfortunally, JSON.stringify()删除反斜杠从这个 \ /日(蜱)\ / 净串行DateTime格式

You are right, @friendlycello. Unfortunally, JSON.stringify() removes backslashes from this \/Date(ticks)\/ .Net serializer DateTime format.

我写了一个自定义函数调节输出从 JSON.stringify(),其中包括这些反斜杠。所以,我可以保持几乎untoched,从仅更换JSON.stringify() customJSONstringify()在<$ C 。$ C> $阿贾克斯()数据:参数

I wrote a custom function that adjusts the output from JSON.stringify(), including these backslashes. So, I can keep almost untoched, only replacing from JSON.stringify() to customJSONstringify() in $.ajax() data: param.

function customJSONstringify(obj) {
    return JSON.stringify(obj).replace(/\/Date/g, "\\\/Date").replace(/\)\//g, "\)\\\/")
}

这篇关于从客户端使用.NET JavaScriptSerializer.Deserialize有日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆