简体   繁体   English

WCF DataContractJsonSerializer 如何反序列化 json DateTime?

[英]How does WCF DataContractJsonSerializer deserialize a json DateTime?

I have 2 datetime json strings, the only difference is the offset part, one is -08, the other one is -09:我有2个日期时间json字符串,唯一的区别是偏移部分,一个是-08,另一个是-09:

"/Date(946713600000-0800)/"
"/Date(946713600000-0900)/"

When I deserialize them using DataContractJsonSerializer当我使用 DataContractJsonSerializer 反序列化它们时

var serializer = new DataContractJsonSerializer(typeof(DateTime));

byte[] byteArray1 = Encoding.UTF8.GetBytes("\"\\/Date(946713600000-0800)\\/\"");
MemoryStream stream1 = new MemoryStream(byteArray1);
var datetime1 = serializer.ReadObject(stream1 );

byte[] byteArray2 = Encoding.UTF8.GetBytes("\"\\/Date(946713600000-0900)\\/\"");
MemoryStream stream2 = new MemoryStream(byteArray2);
var datetime2 = serializer.ReadObject(stream2 );

The result supersize me, datetime1 and datetime2 are exactly the same!结果supersize me,datetime1和datetime2一模一样! both are {1/1/2000 12:00:00 AM}, datetime1.Kind and datetime2.Kind are both Local.两者都是 {1/1/2000 12:00:00 AM},datetime1.Kind 和 datetime2.Kind 都是本地的。

I expect they have one hour difference.我希望他们有一个小时的差异。 Why they are same?为什么他们是一样的?

The time portion is the same value between the two dates.时间部分是两个日期之间的相同值。 The offset, represented as -0900 and -0800, in your example, is just extra information that denotes the offset from UTC that the local time was captured in.在您的示例中,表示为 -0900 和 -0800 的偏移量只是额外信息,表示捕获本地时间的 UTC 偏移量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WCF REST JSON API:DataContractJsonSerializer中的DateTime序列化 - WCF REST JSON API: DateTime serialization in DataContractJsonSerializer 如何使用DataContractJsonSerializer反序列化嵌套的json? - How can I Deserialize nested json with DataContractJsonSerializer? DataContractJsonSerializer用于反序列化JSON - DataContractJsonSerializer to deserialize JSON 如何使用DataContractJsonSerializer序列化/反序列化存储在对象字段中的DateTime? - How to serialize/deserialize a DateTime stored inside an object field using DataContractJsonSerializer? 如何使用JSON.NET或DataContractJsonSerializer反序列化未类型化的对象 - How to deserialize an untyped object using JSON.NET or DataContractJsonSerializer 如何使用DataContractJsonSerializer对字典进行反序列化? - How to deserialize a dictionary using DataContractJsonSerializer? 如何使用DataContractJsonSerializer反序列化特定元素 - How to deserialize specific element with DataContractJsonSerializer WCF DataContractJsonSerializer 并将 DateTime 对象设置为 UTC? - WCF DataContractJsonSerializer and setting DateTime objects to UTC? 使用DataContractJsonSerializer使用不同类型的数组反序列化json - Deserialize json with array of different types using DataContractJsonSerializer 在Windows Phone中使用DataContractJsonSerializer将JSON反序列化为字典 - Deserialize JSON to Dictionary with DataContractJsonSerializer in Windows Phone
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM