Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. C# Deserialization of JSON data - date problem

C# Deserialization of JSON data - date problem

Scheduled Pinned Locked Moved C#
csharpjsonphpsysadminhelp
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 10975497
    wrote on last edited by
    #1

    I've always had a problem with dates, whether it's PHP or C#... And so the program is to use data in the dd.MM.yyyy format And YYY-MM-DD data comes from the API And there is a problem to reconcile it. The data reads correctly from the API. And then it gets shitty. For me, USER DATA should return what I expect, i.e. dd.MM.yyyy but how to make him do it? before deserialization is: Response from the server: [{"id":"1","date":"2016-01-08" User Data from JSON: 0001-01-01, Duration: 0000

    private async Task>> GetBlogPostsAsync()
    {
    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
    var blogPosts = new Dictionary>();

            try
            {
                var client = new RestClient("https://mojlink/");
                var request = new RestRequest("api.php?action=get\_posts", Method.Get);
                var response = await client.ExecuteAsync(request);
    
                Console.WriteLine("Odpowiedź z serwera:");
                Console.WriteLine(response.Content);
    
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    // Sprawdź, czy odpowiedź zawiera poprawne dane JSON
                    if (!string.IsNullOrWhiteSpace(response.Content))
                    {
                        var posts = JsonConvert.DeserializeObject\>(response.Content);
    
                        foreach (var post in posts)
                        {
                            var jsonDate = post.Date.ToString("yyyy-MM-dd");
                            Console.WriteLine($"Data z JSON: {jsonDate}, Czas trwania: {post.Duration}");
    
                            if (DateTime.TryParseExact(jsonDate, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime parsedDate))
                            {
                                if (!blogPosts.ContainsKey(parsedDate))
                                {
                                    blogPosts.Add(parsedDate, new List());
                                }
                                blogPosts\[parsedDate\].Add(post.Duration);
                            }
                            else
                            {
                                // Jeśli nie udało się sparsować daty, zapisz informację do logów
                                Console.WriteLine($"Nieprawidłowy format daty: {post.Date}")
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups