JSInterop.Json.Deserialize is losing milliseconds from DateTime objects

醉酒当歌 提交于 2019-12-11 03:42:57

问题


In my Blazor 0.9.0 project I have a large json string for a list of objects with DateTime property including milliseconds. This is passed to my .cshtml file via Ajax and I call Microsoft.JSInterop.Json.Deserialize on the string. This deserializes well except the DateTime objects lose their milliseconds. For JsonConvert this can be rectified by using JsonSerializerSettings DateParseHandling. How do we do this in Blazor?

var json = "{\"timestamp\":\"2019-03-01T12:22:26.102Z\"}";

I expect this to be deserialized to 01/03/2019 12:22:26:102 but actual result is 01/03/2019 12:22:26


回答1:


Blazor uses SimpleJson, not NewtonSoft. That also affects Http.GetJsonAsync<T>.

I have had it break on me for some rather normal looking Json returned from an API (randomuser.me is an example).

I just added the NewtonSoft package, and used the HttpClient to download the results as a string.




回答2:


Solution was unfortunately having to create a string representation of the Date field to maintain the milliseconds after deserialization. Neither NewtonSoft 12.01 nor JSInterop currently keep milliseconds if implemented within Blazor for some reason.



来源:https://stackoverflow.com/questions/55503163/jsinterop-json-deserialize-is-losing-milliseconds-from-datetime-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!