C
C#3w ago
Zee

How to display api json data into a wpf page

So I am making a flight Booking app and I am using Amadeus Flight API right and I want the user to search for Flight using a Form but I get this odd error Exception: The JSON value could not be converted to System.Collections.Generic.List'1[FakeFlightBookingApp.Model.FlightOffer].Path: $ | LineNumber: 0 | BystePostionLine:1. but it does seem to make the call to the API just fine and the data is displayed in the console log as you can see in the pic. I will give some of my relevant code
29 Replies
Zee
ZeeOP3w ago
Keswiik
Keswiik3w ago
personally, I'd have models made for the API response, then translate those into viewmodels used by your app also you can put cs after your triple backticks for code blocks to give syntax highlighting $code
MODiX
MODiX3w ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Zee
ZeeOP3w ago
using FakeFlightBookingAPI.Services; // Importing the services used in the controller
using Microsoft.AspNetCore.Mvc; // For the base controller class
using System.Net.Http.Headers; // For AuthenticationHeaderValue
using System.Net.Http; // For HttpClient
using System.Threading.Tasks; // For async/await

namespace FakeFlightBookingAPI.Controllers
{
// Define a route for the Flights API controller
[Route("api/[controller]")]
[ApiController] // Indicates that this class is an API controller
public class FlightsController : ControllerBase
{
private readonly FlightOffersSearchService _flightOffersSearchService; // Service for searching flight offers
private readonly AirportLookupService _airportLookupService; // Service for airport lookups

// Constructor to inject the necessary services
public FlightsController(FlightOffersSearchService flightOffersSearchService, AirportLookupService airportLookupService)
{
_flightOffersSearchService = flightOffersSearchService; // Initialize flight search service
_airportLookupService = airportLookupService; // Initialize airport lookup service

}
}
}
using FakeFlightBookingAPI.Services; // Importing the services used in the controller
using Microsoft.AspNetCore.Mvc; // For the base controller class
using System.Net.Http.Headers; // For AuthenticationHeaderValue
using System.Net.Http; // For HttpClient
using System.Threading.Tasks; // For async/await

namespace FakeFlightBookingAPI.Controllers
{
// Define a route for the Flights API controller
[Route("api/[controller]")]
[ApiController] // Indicates that this class is an API controller
public class FlightsController : ControllerBase
{
private readonly FlightOffersSearchService _flightOffersSearchService; // Service for searching flight offers
private readonly AirportLookupService _airportLookupService; // Service for airport lookups

// Constructor to inject the necessary services
public FlightsController(FlightOffersSearchService flightOffersSearchService, AirportLookupService airportLookupService)
{
_flightOffersSearchService = flightOffersSearchService; // Initialize flight search service
_airportLookupService = airportLookupService; // Initialize airport lookup service

}
}
}
Keswiik
Keswiik3w ago
How to deserialize JSON in C# - .NET
Learn how to use the System.Text.Json namespace to deserialize from JSON in .NET. Includes sample code.
Keswiik
Keswiik3w ago
for some examples of how to deserialize json data to a model
Zee
ZeeOP3w ago
thx its odd because on swagger it works fine
Keswiik
Keswiik3w ago
well you are trying to deserialize to a list, and from what I can see in that screenshot the API isn't returning a json list var flightOffers = await response.Content.ReadFromJsonAsync<List<FlightOffer>>(); granted that screenshot isn't formatted at all so it's hard to tell exactly what it's returning
Zee
ZeeOP3w ago
yeah I had it as something else and was still thrwoing the same error asked chatgpt and was saying to make a FlightOffer class
Keswiik
Keswiik3w ago
for starters, format the returned JSON so you can actually make sense of it deserializing to a list is pointless if the returned data isn't a list format and post the whole thing here along with your FlightOffer model
Zee
ZeeOP3w ago
sorry format what exactly
Keswiik
Keswiik3w ago
the JSON returned by that API
Zee
ZeeOP3w ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharedModels
{
public class FlightOffer
{
public string Type { get; set; }
public string Id { get; set; }
public List<Itinerary> Itineraries { get; set; } // Define Itineraries property
public Price Price { get; set; } // Assuming Price is another class defined elsewhere
}

public class Itinerary
{
public string Duration { get; set; }
public List<Segment> Segments { get; set; } // Define Segments property
}

public class Segment
{
public Departure Departure { get; set; }
public Arrival Arrival { get; set; }
public string CarrierCode { get; set; }
public string Number { get; set; }
// Add other properties as needed
}

public class Departure
{
public string IataCode { get; set; }
public DateTime At { get; set; } // Use DateTime for proper handling of date
}

public class Arrival
{
public string IataCode { get; set; }
public DateTime At { get; set; } // Use DateTime for proper handling of date
}

public class Price
{
public string Currency { get; set; }
public string Total { get; set; }
// Add other properties as needed
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SharedModels
{
public class FlightOffer
{
public string Type { get; set; }
public string Id { get; set; }
public List<Itinerary> Itineraries { get; set; } // Define Itineraries property
public Price Price { get; set; } // Assuming Price is another class defined elsewhere
}

public class Itinerary
{
public string Duration { get; set; }
public List<Segment> Segments { get; set; } // Define Segments property
}

public class Segment
{
public Departure Departure { get; set; }
public Arrival Arrival { get; set; }
public string CarrierCode { get; set; }
public string Number { get; set; }
// Add other properties as needed
}

public class Departure
{
public string IataCode { get; set; }
public DateTime At { get; set; } // Use DateTime for proper handling of date
}

public class Arrival
{
public string IataCode { get; set; }
public DateTime At { get; set; } // Use DateTime for proper handling of date
}

public class Price
{
public string Currency { get; set; }
public string Total { get; set; }
// Add other properties as needed
}
}
yeah I will do that now swagger does it automatically
Keswiik
Keswiik3w ago
:PepoSalute: got a feeling your models aren't right but won't be able to say why until that json is readable
Zee
ZeeOP3w ago
yeah it takes some time for the swagger to do its thing
Keswiik
Keswiik3w ago
you can also copy-paste the unformatted response into vscode, then format with vscode
Zee
ZeeOP3w ago
here u go
Keswiik
Keswiik3w ago
yeah, you are missing an additional model to handle the overall response object your models only cover what is nested within the data section of that payload
Zee
ZeeOP3w ago
oh so I need one for meta?
Keswiik
Keswiik3w ago
no, you need one for the entire response object
Keswiik
Keswiik3w ago
No description
Keswiik
Keswiik3w ago
the circled part is what your models cover, which assumes your data comes in the form of
[
flight data,
more data,
etc
]
[
flight data,
more data,
etc
]
but you need to handle
{
"data": [
"flight data",
"more data",
"etc"
]
}
{
"data": [
"flight data",
"more data",
"etc"
]
}
Zee
ZeeOP3w ago
so I need source, instantTicketingRequired too etc
Keswiik
Keswiik3w ago
unsure exactly how ReadResponseAsJsonAsync or whatever it was named handles missing keys / properties, but you can probably ignore stuff like that the important part is having a model with a list of flight offers inside of it since that is exactly what "data" is in your json
Zee
ZeeOP3w ago
ok thank you I get that will look into thx
Keswiik
Keswiik3w ago
gl
Zee
ZeeOP3w ago
it works man th tgx thx
Keswiik
Keswiik3w ago
:PepoSalute:
Want results from more Discord servers?
Add your server