Десериализация данных JSON не работаетC#

Место общения программистов C#
Ответить
Anonymous
 Десериализация данных JSON не работает

Сообщение Anonymous »

Я пытаюсь ввести данные json в модель.
Мой «flightSearchResult» возвращает значение null.
Я не уверен, но я бы сказал, что проблема в дезерализации или моя модель не очень хорошая.
Формат Json действителен, как я вижу в терминале, и статус 200 ОК.

Код: Выделить всё

var url = $"/v2/shopping/flight-offers?originLocationCode={originLocationCode}&destinationLocationCode={destinationLocationCode}&departureDate={departureDate}&returnDate={returnDate}&adults={adults}&currencyCode={currencyCode}";
var message = new HttpRequestMessage(HttpMethod.Get, url);

var response = await http.SendAsync(message);

response.EnsureSuccessStatusCode();

using var stream = await response.Content.ReadAsStreamAsync();

var rawResponse = await response.Content.ReadAsStringAsync();
Console.WriteLine($"Raw API Response: {rawResponse}");

// Deserialize the API response into the model

var flightSearchResult = await JsonSerializer.DeserializeAsync(stream);
Модель FlightSearchResult

Код: Выделить всё

public class FlightSearchResult
{
public List Data { get; set; }
}

public class FlightOffer
{
public string Type { get; set; }
public string Id { get; set; }
public string Source { get; set; }
public bool InstantTicketingRequired { get; set; }
public bool NonHomogeneous { get; set; }
public bool OneWay { get; set; }
public bool IsUpsellOffer { get; set; }
public string LastTicketingDate { get; set; }
public string LastTicketingDateTime { get; set; }
public int NumberOfBookableSeats { get; set; }
public List Itineraries { get; set; }
public Price Price { get; set; }
public PricingOptions PricingOptions { get; set; }
public List ValidatingAirlineCodes { get; set; }
public List TravelerPricings { get; set; }
}

public class Itinerary
{
public string Duration { get; set; }
public List Segments { get; set; }
}

public class Segment
{
public Departure Arrival { get; set; }
public Departure Departure { get; set; }
public string CarrierCode { get; set; }
public string Number { get; set; }
public Aircraft Aircraft { get; set; }
public Operating Operating { get; set; }
public string Duration { get; set; }
public string Id { get; set; }
public int NumberOfStops { get; set; }
public bool BlacklistedInEU { get; set; }
}

public class Departure
{
public string IataCode { get; set; }
public string Terminal { get; set; }
public string At { get; set; }
}

public class Aircraft
{
public string Code { get; set; }
}

public class Operating
{
public string CarrierCode { get; set; }
}

public class Price
{
public string Currency { get; set; }
public string Total { get; set; }
public string Base { get; set; }
public List Fees { get; set; }
public string GrandTotal { get; set; }
}

public class Fee
{
public string Amount { get; set; }
public string Type { get; set; }
}

public class PricingOptions
{
public List FareType { get; set; }
public bool IncludedCheckedBagsOnly { get; set; }
}

public class TravelerPricing
{
public string TravelerId { get; set; }
public string FareOption { get; set; }
public string TravelerType { get; set; }
public Price Price { get; set; }
public List FareDetailsBySegment { get; set; }
}

public class FareDetail
{
public string SegmentId { get; set; }
public string Cabin { get; set; }
public string FareBasis { get; set; }
public string Class { get; set; }
public IncludedCheckedBags IncludedCheckedBags { get; set; }
}

public class IncludedCheckedBags
{
public int Weight { get; set; }
public string WeightUnit { get; set;  }
}
Формат Json

Код: Выделить всё

{
"meta": {
"count": 59,
"links": {
"self": "https://test.api.amadeus.com/v2/shopping/flight-offers?originLocationCode=SYD&destinationLocationCode=BKK&departureDate=2024-12-29&adults=2&currencyCode=EUR"
}
},
"data": [
{
"type": "flight-offer",
"id": "1",
"source": "GDS",
"instantTicketingRequired": false,
"nonHomogeneous": false,
"oneWay": false,
"isUpsellOffer": false,
"lastTicketingDate": "2024-12-16",
"lastTicketingDateTime": "2024-12-16",
"numberOfBookableSeats": 9,
"itineraries": [
{
"duration": "PT14H30M",
"segments": [
{
"departure": {
"iataCode": "SYD",
"terminal": "0",
"at": "2024-12-29T07:15:00"
},
"arrival": {
"iataCode": "DPS",
"terminal": "0",
"at": "2024-12-29T10:45:00"
},
"carrierCode": "OD",
"number": "172",
"aircraft": {
"code": "738"
},
"operating": {
"carrierCode": "OD"
},
"duration": "PT6H30M",
"id": "53",
"numberOfStops": 0,
"blacklistedInEU": false
},
{
"departure": {
"iataCode": "DPS",
"terminal": "D",
"at": "2024-12-29T14:15:00"
},
"arrival": {
"iataCode": "DMK",
"terminal": "0",
"at": "2024-12-29T17:45:00"
},
"carrierCode": "ID",
"number": "7637",
"aircraft": {
"code": "738"
},
"operating": {
"carrierCode": "ID"
},
"duration": "PT4H30M",
"id": "54",
"numberOfStops": 0,
"blacklistedInEU": false
}
]
}
],
"price": {
"currency": "EUR",
"total": "483.86",
"base": "324.00",
"fees": [
{
"amount": "0.00",
"type": "SUPPLIER"
},
{
"amount": "0.00",
"type": "TICKETING"
}
],
"grandTotal": "483.86"
},
"pricingOptions": {
"fareType": [
"PUBLISHED"
],
"includedCheckedBagsOnly": false
},
"validatingAirlineCodes": [
"GP"
],
"travelerPricings":  [
{
"travelerId": "1",
"fareOption": "STANDARD",
"travelerType": "ADULT",
"price": {
"currency": "EUR",
"total": "241.93",
"base": "162.00"
},
"fareDetailsBySegment": [
{
"segmentId": "53",
"cabin": "ECONOMY",
"fareBasis": "XOWAU",
"class": "X",
"includedCheckedBags": {
"weight": 0,
"weightUnit": "KG"
}
},
{
"segmentId": "54",
"cabin": "ECONOMY",
"fareBasis": "XOWID",
"class": "X",
"includedCheckedBags": {
"weight": 0,
"weightUnit": "KG"
}
}
]
},
{
"travelerId": "2",
"fareOption": "STANDARD",
"travelerType": "ADULT",
"price": {
"currency": "EUR",
"total": "241.93",
"base": "162.00"
},
"fareDetailsBySegment": [
{
"segmentId": "53",
"cabin": "ECONOMY",
"fareBasis": "XOWAU",
"class": "X",
"includedCheckedBags": {
"weight": 0,
"weightUnit": "KG"
}
},
{
"segmentId": "54",
"cabin": "ECONOMY",
"fareBasis": "XOWID",
"class": "X",
"includedCheckedBags": {
"weight": 0,
"weightUnit": "KG"
}
}
]
}
]
},
{
В чем может быть проблема? Что-то в контроллере? Моя модель не очень хорошая?

Подробнее здесь: https://stackoverflow.com/questions/792 ... oesnt-work
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»