0

I'm trying to retrieve de results from the latest F1 race with this endpoint: http://ergast.com/api/f1/current/last/results. I did get the results when trying a GET call in Postman. At this moment I only get the following result in my console: https://i.stack.imgur.com/nEios.png I want the GivenName and FamilyName of the 3 (or all) first finished drivers printed back to the console. For testing purposes I left out the text service API, and focused solely on printing te results to the console, that's why you'll find this in my code. The code I'm working on:

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;

namespace Software_Test
{
    class Program
    {
        static async Task Main(string[] args)
        {
            // Informatie voor het versturen van SMS
            string apiKey = "//";
            string sender = "//";
            string receiver = "//";

            // Maak een HTTP-client aan voor het ophalen van de F1-racegegevens
            HttpClient f1Client = new HttpClient();

            // Ophalen van de uitslag van de laatste F1-race van de Ergast API
            string ergastApiUrl = "http://ergast.com/api/f1/current/last/results.json";
            var f1Response = await f1Client.GetAsync(ergastApiUrl);

            // Controleer het antwoord van de Ergast API
            if (f1Response.IsSuccessStatusCode)
            {
                // Lees de JSON-respons van de Ergast API
                var f1Result = await f1Response.Content.ReadAsStringAsync();
                // Verkrijg de gegeven naam, achternaam en positie van de eerste drie coureurs uit de JSON-respons
                var raceResults = ParseRaceResultsFromJson(f1Result);

                // Weergeven van de gegevens van de eerste drie coureurs
                Console.WriteLine("Dit is de uitslag van de laatste F1-race:");
                for (int i = 0; i < Math.Min(3, raceResults.Count); i++)
                {
                    string driverName = $"{raceResults[i].GivenName} {raceResults[i].FamilyName}";
                    string position = $"{i + 1}";

                    Console.WriteLine($"{position}. {driverName}");
                }
            }
            else
            {
                Console.WriteLine("Fout bij het ophalen van F1-racegegevens van de Ergast API.");
            }
        }

        static List<RaceResult> ParseRaceResultsFromJson(string json)
        {
            List<RaceResult> raceResults = new List<RaceResult>();

            // Deserialiseer de JSON-respons naar de modelklassen
            var jsonObject = JsonSerializer.Deserialize<RootObject>(json);

            var results = jsonObject.MRData.RaceTable.Races[0].Results;

            foreach (var result in results)
            {
                raceResults.Add(new RaceResult
                {
                    GivenName = result.Driver.GivenName,
                    FamilyName = result.Driver.FamilyName,
                    Result = result.Position
                });
            }

            return raceResults;
        }
    }

    // Modelklasse voor het opslaan van de race-uitslag
    public class RaceResult
    {
        public string GivenName { get; set; }
        public string FamilyName { get; set; }
        public string Result { get; set; }
    }

    // Modelklassen voor het deserialiseren van de JSON-respons
    public class RootObject
    {
        public MRData MRData { get; set; }
    }

    public class MRData
    {
        public RaceTable RaceTable { get; set; }
    }

    public class RaceTable
    {
        public List<Race> Races { get; set; }
    }

    public class Race
    {
        public List<Result> Results { get; set; }
    }

    public class Result
    {
        public Driver Driver { get; set; }
        public string Position { get; set; }
    }

    public class Driver
    {
        public string GivenName { get; set; }
        public string FamilyName { get; set; }
    }
}

dbc
  • 104,963
  • 20
  • 228
  • 340
Youritj
  • 1
  • 4
  • 2
    This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David May 22 '23 at 20:03
  • In the json there is no `"GivenName"` property. It's `"givenName"`. It is case sensitive. Same for `"familyName"`. – 001 May 22 '23 at 20:39
  • @001 Thanks for the response, but I used the same property throughout the code, or am I missing something? It's called "GivenName" and "FamilyName" in my code, as for the reponse body you get from the GET call to the API endpoint I used. – Youritj May 22 '23 at 20:56
  • I'm saying that in the json (from the url in `ergastApiUrl`), the properties are called `"givenName"` and `"familyName"`. You need to change your `class Driver` to be `public string givenName { get; set; }` and `public string familyName { get; set; }`. – 001 May 22 '23 at 21:12
  • 1
    Another option is [How to customize property names and values with System.Text.Json](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/customize-properties?pivots=dotnet-7-0) – 001 May 22 '23 at 21:12

0 Answers0