First time working with JSON in C#. I'm building a Blazor app using Amadeus API data (flights search) and I cannot seem to be able to use the JSON object in a Blazor component.
The error I'm getting is "Unhandled exception rendering component: The JSON value could not be converted to System.Collections.Generic.List`1[Flights.Models.RootObject]" which I think might be because I'm not building my C# classes correctly.
Here's a sample of the JSON object, not all but with the needed information:
{"data": [ {"type": "flight-destination","origin": "XOC","destination": "YJB","departureDate": "2025-08-19","returnDate": "2025-08-21","price": {"total": "60.0" },"links": {"flightDates": "https://test.api.amadeus.com/v1/shopping/flight-dates?origin=XOC&destination=YJB&departureDate=2025-08-19,2025-08-21&oneWay=false&nonStop=false&viewBy=DESTINATION","flightOffers": "https://test.api.amadeus.com/v2/shopping/flight-offers?originLocationCode=XOC&destinationLocationCode=YJB&departureDate=2025-08-19&returnDate=2025-08-21&adults=1&nonStop=false&viewBy=DESTINATION" } }, {"type": "flight-destination","origin": "MAD","destination": "AUH","departureDate": "2025-07-22","returnDate": "2025-07-28","price": {"total": "679.07" },"links": {"flightDates": "https://test.api.amadeus.com/v1/shopping/flight-dates?origin=MAD&destination=AUH&departureDate=2025-07-22,2025-07-28&oneWay=false&nonStop=false&viewBy=DESTINATION","flightOffers": "https://test.api.amadeus.com/v2/shopping/flight-offers?originLocationCode=MAD&destinationLocationCode=AUH&departureDate=2025-07-22&returnDate=2025-07-28&adults=1&nonStop=false&viewBy=DESTINATION" } } ],"dictionaries": {"currencies": {"EUR": "Euro" },"locations": {"MXP": {"subType": "AIRPORT","detailedName": "MALPENSA" },"AMS": {"subType": "AIRPORT","detailedName": "SCHIPHOL AIRPORT" } } },"meta": {"currency": "EUR","links": {"self": "https://test.api.amadeus.com/v1/shopping/flight-destinations?origin=MAD&departureDate=2025-07-16,2026-01-10&oneWay=false&nonStop=false&viewBy=DESTINATION" },"defaults": {"departureDate": "2025-07-16,2026-01-10","oneWay": false,"duration": "1,15","nonStop": false,"viewBy": "DESTINATION" } },"warnings": []}I then tried to mirror my C# classes to match the JSOn structure:
namespace Flights.Models{ public class RootObject { public Data[] data { get; set; } public Dictionaries dictionaries { get; set; } public Meta meta { get; set; } public object[] warnings { get; set; } } public class Data { public string type { get; set; } public string origin { get; set; } public string destination { get; set; } public string departureDate { get; set; } public string returnDate { get; set; } public Price price { get; set; } public Links links { get; set; } } public class Price { public string total { get; set; } } public class Links { public string flightDates { get; set; } public string flightOffers { get; set; } } public class Dictionaries { public Currencies currencies { get; set; } } public class Currencies { public string EUR { get; set; } } public class Meta { public string currency { get; set; } public Links1 links { get; set; } public Defaults defaults { get; set; } } public class Links1 { public string self { get; set; } } public class Defaults { public string departureDate { get; set; } public bool oneWay { get; set; } public string duration { get; set; } public bool nonStop { get; set; } public string viewBy { get; set; } }}And the method I'm using to get the data into the destinations field so I can pass this one into a children component:
private List<RootObject> destinations;private async Task<List<RootObject>?> GetMostTravelledDestinations(string accessToken) { var request = new HttpRequestMessage(HttpMethod.Get, "https://test.api.amadeus.com/v1/shopping/flight-destinations?origin=MAD"); request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken); request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); var response = await Http.SendAsync(request); if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync(); var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; var parsed = JsonSerializer.Deserialize<List<RootObject>>(json, options); if (parsed != null) { foreach (var rootObject in parsed) { Console.WriteLine($"{rootObject.data}"); } destinations = parsed; using var doc = JsonDocument.Parse(json); var root = doc.RootElement; if (root.TryGetProperty("data", out var dataArray)) { Console.WriteLine($"Data contains {dataArray.GetArrayLength()} items."); } else { Console.WriteLine("No 'data' property found."); } return parsed; } } return new List<RootObject>(); }