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; }
}
}