在C#中反序列化JSON日期的问题-增加2小时 [英] Problem deserializating JSON Date in C# - adding 2 hours

查看:277
本文介绍了在C#中反序列化JSON日期的问题-增加2小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将JSON日期反序列化为C#DateTime时,我们遇到了一个令人讨厌的问题.

We are having such a nasty problem when deserializating a JSON date to a C# DateTime.

代码是:

JavaScriptSerializer serializer = new JavaScriptSerializer();
jsonTrechos = jsonTrechos.Replace("/Date(", "\\/Date(").Replace(")/", ")\\/");
Trecho[] model = serializer.Deserialize<Trecho[]>(jsonTrechos);

jsonTrechos是json2.js的JSON.stringify();的字符串.

The jsonTrechos is a string of json2.js's JSON.stringify();.

问题是:反序列化的工作原理是,将Trechos对象的所有日期都添加2小时.

Problem is: the deserialization works, bur all dates of the Trechos objects are added with 2 hours.

我的时区是巴西(UTC -3),并且我们有夏令时(因此我们目前使用的是UTC -2).我想也许本地化和时区可能在其中起作用,如果确实如此,我不知道如何解决.

My timezone is Brazil (UTC -3) and we're under daylight savings (so we're currently on UTC -2), if it has anything to do. I guess that perhaps localization and timezones may be playing a part on this and if they really are, I have no idea on how to fix it.

推荐答案

日期对象,在JSON中表示为 "/日期(滴答数)/".这 滴答数为正或 负的long值,指示 的刻度数(毫秒) 自午夜 01起已过去 1970年1月UTC.

Date object, represented in JSON as "/Date(number of ticks)/". The number of ticks is a positive or negative long value that indicates the number of ticks (milliseconds) that have elapsed since midnight 01 January, 1970 UTC.

尝试致电DateTime.ToLocalTime(),看看您是否有正确的约会日期.

Try calling DateTime.ToLocalTime() and see if you get the correct date for you.

这篇关于在C#中反序列化JSON日期的问题-增加2小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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