© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
6 replies
Tostisto

Problem with json deserialization

I have an issue with deserialization of the datetime from a JSON string.

I need to deserialize
2024-07-09T10:36:23:000Z
2024-07-09T10:36:23:000Z
into a datetime object, but currently, I am having a problem with deserialization.

My code looks like this:

using System;
using System.Text.Json;

string jsonString = "{\"Date\":\"2024-07-09T10:36:23:000Z\",\"Message\":\"Hello, World!\"}";

MyData? data = JsonSerializer.Deserialize<MyData>(jsonString);

Console.WriteLine(data.Date);  // Output: 7/9/2024 10:36:23 AM

public class MyData
{
    public DateTime Date { get; set; }
    public string Message { get; set; }
}
using System;
using System.Text.Json;

string jsonString = "{\"Date\":\"2024-07-09T10:36:23:000Z\",\"Message\":\"Hello, World!\"}";

MyData? data = JsonSerializer.Deserialize<MyData>(jsonString);

Console.WriteLine(data.Date);  // Output: 7/9/2024 10:36:23 AM

public class MyData
{
    public DateTime Date { get; set; }
    public string Message { get; set; }
}


With this error

System.Text.Json.JsonException: The JSON value could not be converted to System.DateTime. Path: $.Date | LineNumber: 0 | BytePositionInLine: 34.
 ---> System.FormatException: The JSON value is not in a supported DateTime format.
System.Text.Json.JsonException: The JSON value could not be converted to System.DateTime. Path: $.Date | LineNumber: 0 | BytePositionInLine: 34.
 ---> System.FormatException: The JSON value is not in a supported DateTime format.


Shoud be problem in culture setting in os? Or how can I serialize it without creating a new converter?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

JSON Deserialization Multiple Entries
C#CC# / help
2y ago
Custom Newtonsoft JSON deserialization
C#CC# / help
2y ago
JSON deserialization (proper way?)
C#CC# / help
3y ago
Deserialization of json object
C#CC# / help
3y ago