使用以下格式转换日期时间字符串:(yyyy-MM-dd'T'hh:mm:ss-zzz) [英] Convert datetime string with this format: (yyyy-MM-dd'T'hh:mm:ss-zzz)

查看:597
本文介绍了使用以下格式转换日期时间字符串:(yyyy-MM-dd'T'hh:mm:ss-zzz)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个包含日期的JSON字符串,该日期看起来像这样:2015-07-09T08:38:49-07:00其中最后一部分是时区.是否有将其转换为DateTimeOffset的标准方法?

I am receiving a JSON string that contains a date that looks like this: 2015-07-09T08:38:49-07:00 where the last part is the timezone. Is there a standard way to convert this to a DateTimeOffset?

这是我到目前为止所拥有的:

Here is what I have so far:

var olu = JsonConvert.DeserializeObject<OneLoginUser>(jToken.ToString(), new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd'T'HH:mm:sszzz" });

这不会反序列化任何日期.我曾尝试使用-Zhh:mm作为时区数据,但似乎无法反序列化任何日期.

This doesn't deserialize any of the dates. I've tried using -Z and hh:mm for the timezone data, but I can't seem to deserialize any of the dates.

作为参考,这来自SSO提供者OneLogin. 这是指向用户文档的链接.注意顶部的日期.

For reference, this is from OneLogin, a SSO provider. Here's a link to the user documentation. Notice the bit about the dates at the top.

推荐答案

这是标准 ISO 8601 带有偏移量的扩展格式时间戳,也被 RFC 3339 涵盖.没什么特别的.

That is a standard ISO 8601 extended format timestamp with offset, also covered by RFC 3339. There's nothing special about it.

DateTimeOffset.Parse("2015-07-09T08:38:49-07:00")

DateTimeOffset.ParseExact("2015-07-09T08:38:49-07:00", "yyyy-MM-dd'T'HH:mm:sszzz",
                                                       CultureInfo.InvariantCulture)

使用JSON.Net,默认值应该可以正常工作.无需指定任何特殊内容.

With JSON.Net, the defaults should work well. No need to specify anything special.

JsonConvert.DeserializeObject<DateTimeOffset>("\"2015-07-09T08:38:49-07:00\"")

问题注释中的小提琴布赖恩(aiddle Brian)发布表明,该序列在反序列化较大对象时有效.如果仍然无法使用它,也许您可​​以编辑问题以显示要反序列化的特定JSON以及要放入其中的对象结构.

The fiddle Brian posted in the question comments shows that it works when deserializing a larger object. If you're still not getting it to work, perhaps you could edit your question to show the specific JSON you're trying to deserialize and the object structure you're putting it into.

我注意到您的代码的一件事,您显示了来自jToken.ToString()的json,因此您必须事先使用JObject.Parse对其进行了解析.这样做只是将其转换回json并反序列化,这有点奇怪.使用JsonConvert.DeserializeObject直接从json字符串转到实体,或者如果由于其他原因已经从jToken开始使用jToken.ToObject<OneLoginUser>().无需混合使用这两种API,并且您可能会在设置过程中失去日期/时间信息,这取决于您的设置.

One thing I noticed about your code, you show the json coming from jToken.ToString(), so somewhere you must have previously parsed using JObject.Parse. It's a little strange to do that, just to convert back to json and deserialize. Either go directly from the json string to the entity using JsonConvert.DeserializeObject, or use jToken.ToObject<OneLoginUser>() if you're already starting with jToken for some other reason. No need to mix both APIs, and it's possible you're loosing the date/time information in the process depending on what your settings are.

这篇关于使用以下格式转换日期时间字符串:(yyyy-MM-dd'T'hh:mm:ss-zzz)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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