简体   繁体   English

将 json utc 日期从 jira api 反序列化为 .net 日期时间

[英]Deserialize json utc date from jira api to .net datetime

Jira is responding to a GET Request with this date time format: Jira 正在使用此日期时间格式响应 GET 请求:

"2013-09-17T12:07:36.000-0500" “2013-09-17T12:07:36.000-0500”

I've gotten this far: settings.DateTimeFormat = new DateTimeFormat("yyyy-MM-ddTHH:mm:ss.fff");我已经走了这么远: settings.DateTimeFormat = new DateTimeFormat("yyyy-MM-ddTHH:mm:ss.fff");

  • 1). 1)。 Will I need to escape the 'T'?我需要逃避'T'吗? (\\T) (\\T)
  • 2). 2)。 I don't know what to do for the '-0500'我不知道如何处理“-0500”
    • a) It looks like zzz might work but that gets me '-05:00' a) 看起来 zzz 可能会起作用,但这让我'-05:00'
    • b) I've tried this and it didnt work: "yyyy-MM-ddTHH:mm:ss.fffZ" b)我试过这个,但没有用:“yyyy-MM-ddTHH:mm:ss.fffZ”

This is a classical XY-Problem . 这是经典的XY问题 ( X would be: how can I process this json response ) X将是: 我该如何处理这个json响应

See how it can be done easily with Json.Net 了解如何使用Json.Net轻松完成

WebClient wc = new WebClient();
string json = wc.DownloadString("https://jira.atlassian.com/rest/api/latest/issue/JRA-9.json");
dynamic jObj = JObject.Parse(json);
DateTime dt = (DateTime)jObj.fields.updated;

Of course, deserializing the json string to concrete classes is also possible. 当然,也可以将json字符串反序列化为具体的类。 In that case you can use this site 在这种情况下,您可以使用此网站

Does this snippet solves your issue? 这个摘要是否可以解决您的问题?

using System.Web.Script.Serialization;

var json = @"{ date : ""2013-09-17T12:07:36.000-0500""}";
var date = new JavaScriptSerializer().Deserialize<Dictionary<string, DateTime>>(json);

试试“yyyy-MM-dd'T'HH:mm:ss.FFFK”

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM