Fixing JSON Date serialization from .Net web method(从 .Net Web 方法修复 JSON 日期序列化)
问题描述
我目前正在开发一个项目,我通过 ajax 将 .Net 类型发送到客户端应用程序.我对被序列化并设置给客户端的对象没有任何问题.
I am currently working on a project where I am sending a .Net type via ajax to a client application via ajax. I have no issues with the object being serialized and set to the client.
当我获取完全相同的对象并通过 Web 方法将其发回服务器时遇到问题,并出现以下错误:/Date(1373950800000)/不是 DateTime 的有效值. 这很烦人,因为这是微软给我的方式,但这不是重点.
I run into issues when I take the exactly same object and post it back to the server via a web method with the following error: /Date(1373950800000)/ is not a valid value for DateTime. Which is pretty annoying as that is how Microsoft gave it to me, but that's besides the point.
有没有人可以快速解决这个问题?我想要一种无缝方式,无需在从 ajax 调用返回对象之前立即更改对象.
Does anyone have a quick fix for this? I want a seamless way this can be accomplished without having to change the object right before returning it from the ajax call.
推荐答案
您的问题归结为您使用的服务器端 JavaScript 序列化程序;JsonDataContractSerializer(ASP.NET MVC 的默认序列化器)或 NewtonSoft Json Serializer(ASP.NET Web API 的默认序列化器).
Your issue comes down to the server-side JavaScript serializer you are using; either JsonDataContractSerializer (default serializer for ASP.NET MVC) or NewtonSoft Json Serializer (default serializer for ASP.NET Web API).
有关此日期修改问题的直观示例以及可能的解决方案,请查看 ASP.NET MVC 和 Web API 中的 JSON 日期不同.
For a visual example of this date mangling issue as well as possible solutions, check out JSON Dates are Different in ASP.NET MVC and Web API.
这篇关于从 .Net Web 方法修复 JSON 日期序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 .Net Web 方法修复 JSON 日期序列化
基础教程推荐
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
