问题
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