为什么RestSharp反序列化两个日期的方式不同? [英] Why is RestSharp deserializing two dates differently?

查看:151
本文介绍了为什么RestSharp反序列化两个日期的方式不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个休息电话,它会返回此电话(使用Chrome中的Advance Rest Client进行测试):

I have a rest call that returns this (using Advance Rest Client in Chrome to do testing):

MyObject: [22]
0:  {
ID: "123456"
UTC1: "2013-04-19T03:12:32Z"
UTC2: "2013-04-19T03:12:36.994Z"
}

捕获响应并将其序列化为对象的代码看起来像这样:

The code that grabs the response and serializes it to an object looks like this:

IRestResponse<List<MyObject>> response = client.Execute<List<MyObject>>(request);

当我查看响应对象时,其中一个日期是错误的。如果我检查它或以任何方式使用对象,我将得到以下信息:

When I look at the response object one of the dates is wrong. If I inspect it or use the objects in any way I get this:

UTC1: 4/19/2013 3:12     
UTC2: 4/18/2013 9:12:36 PM <--CONVERTED!!

我需要将两者都序列化为响应中返回的时间,而不是从UTC /格林尼治标准时间到当地时间。如您所见,上面的一个值保留其UTC值,而另一个则转换为我的时区。我发现两者都通过Convert.DateTime函数运行,但是如果我使用字符串进行操作,则两个值都将转换为本地时间。我意识到原始值之一(正在转换的值)并不完全遵循ISO 8601格式(精度太高);不幸的是,这是我现在必须使用的数据。

I need both to be serialized as the time that is returned in the response, not converted from UTC/GMT to local time. As you can see above one value keeps its UTC value while the other gets converted to my timezone. I figured that both were being run through the Convert.DateTime function but if I do that with the strings both values come out as converted to local time. I realize that one fo the original values (the one that is getting converted) doesn't exactly obey ISO 8601 format (too much precision); unfortunately that is the data I have to work with for now.

有人可以告诉我如何强制RestSharp确保两个日期都在UTC内吗?

Can anyone tell me how to force RestSharp to make sure both dates are in UTC?

推荐答案

使用 Json .NET 进行反序列化,而不是内置的RestSharp反序列化器。

Use Json.NET to do the deserialization instead of the built in RestSharp deserializer.

response = client.Execute(request);    
var myObjects = JsonConvert.Deserialize<List<MyObject>>(response)

这篇关于为什么RestSharp反序列化两个日期的方式不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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