微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将unix时间戳转换为正常dateUWP

我有UWP应用程序,我得到我的JSON,写入列表和Binf视图

这里是视图模型的代码

public class TopPostsviewmodel : INotifyPropertyChanged { private List<Child> postsList; public List<Child> PostsList { get { return postsList; } set { postsList = value; OnPropertyChanged(); } } public TopPostsviewmodel() { Posts_download(); } public async void Posts_download() { string url = "https://www.reddit.com/top/.json?count=50"; var json = await FetchAsync(url); RootObject rootObjectData = JsonConvert.DeserializeObject<RootObject>(json); PostsList = new List<Child>(rootObjectData.data.children); } private async Task<string> FetchAsync(string url) { string jsonString; using (var httpClient = new System.Net.Http.HttpClient()) { var stream = await httpClient.GetStreamAsync(url); StreamReader reader = new StreamReader(stream); jsonString = reader.ReadToEnd(); } return jsonString; }

在json中我有data.created_utc字段。 例如它就像这个created_utc: 1446179731 。 我读到这是unix timestamp

这是我的课程: 课程

我们可以在我们的安装包中捆绑mono吗?

在哪里可以设置Windows XP中CurrentUICulture的初始值?

有没有办法使应用程序平台的中间代码独立?

如何将目录中的内容添加到C#中的ListBox

Windows – 用C#命名pipe道性能build议

我需要将其转换为正常date。 我如何做到这一点,并写回列表?

所以我的问题更多的是,如何把领域和写回来。

是否有任何使用.NET的自动化testing示例源代码

将两个RichTextBox内容附加为单个RichTextstring

如何在.NET应用程序中从FTP协议获取目录文件大小

如何编译使用VS2010的Windows x64位的Leptonica库?

Visual Studio ClickOnce部署是否自动包含必要的.NET框架?

您将不得不在Data2类中添加一个属性获取DateTime中的转换数据。

[JsonIgnore] public DateTime created_utc { get { var unix = new DateTime(1970,1,DateTimeKind.Utc); return unix.AddSeconds(this.created_utcUnix); } set { var unix = new DateTime(1970,DateTimeKind.Utc); this.created_utcUnix = Convert.ToInt64((value - unix).TotalSeconds); } } [JsonProperty("created_utc")] public double created_utcUnix { get; set; }

现在,您可以在客户端代码中始终使用created_utcCustom

代码: https : //pastebin.com/A4GReYHj

@saurabh有一个方法。 但我想你可能想转换它,不要改变类。

你可以使用JsonConverter来转换它。

public class Data2 { [JsonConverter(typeof(UnixConvert))] public DateTime created_utc{set;get;} }

我有一堂上课。 foo类有一个名为created_utc的属性

class Foo { [JsonConverter(typeof(UnixConvert))] public DateTime created_utc { set; get; } }

UnixConvert是一个JsonConverter

class UnixConvert : JsonConverter { public override void WriteJson(JsonWriter writer,object value,JsonSerializer serializer) { var time = ToUnixTimestamp((DateTime) value); writer.WriteValue(time); } public override object ReadJson(JsonReader reader,Type objectType,object existingValue,JsonSerializer serializer) { long unixTimeStamp = long.Parse(reader.Value.ToString()); return UnixTimeStampToDateTime(unixTimeStamp); } public override bool CanConvert(Type objectType) { return true; } private static DateTime UnixTimeStampToDateTime(long unixTimeStamp) { System.DateTime dtDateTime = new System.DateTime(1970,0); dtDateTime = dtDateTime.AddSeconds(unixTimeStamp); return dtDateTime; } public static long ToUnixTimestamp(DateTime time) { var date = new DateTime(1970,time.Kind); var unixTimestamp = System.Convert.ToInt64((time - date).TotalSeconds); return unixTimestamp; } }

我写了一个测试代码

Foo foo = new Foo() { created_utc = DateTime.Now }; var str = JsonConvert.SerializeObject(foo); foo = JsonConvert.DeserializeObject<Foo>(str);

我可以看到它可以运行。

请参阅https://stackoverflow.com/a/28305303/6116637

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐