I have a Json as below.
{
"currency": "MYR",
"results": [
{
"itineraries": [
{
"outbound": {
"flights": [
{
"departs_at": "2018-06-03T06:25",
"arrives_at": "2018-06-03T07:25",
"origin": {
"airport": "PEN"
},
"destination": {
"airport": "KUL",
"terminal": "M"
},
"marketing_airline": "OD",
"operating_airline": "OD",
"flight_number": "2105",
"aircraft": "738",
"booking_info": {
"travel_class": "ECONOMY",
"booking_code": "Q",
"seats_remaining": 9
}
}
]
},
"inbound": {
"flights": [
{
"departs_at": "2018-06-04T14:10",
"arrives_at": "2018-06-04T15:10",
"origin": {
"airport": "KUL",
"terminal": "M"
},
"destination": {
"airport": "PEN"
},
"marketing_airline": "OD",
"operating_airline": "OD",
"flight_number": "2108",
"aircraft": "739",
"booking_info": {
"travel_class": "ECONOMY",
"booking_code": "O",
"seats_remaining": 5
}
}
]
}
}
],
"fare": {
"total_price": "360.00",
"price_per_adult": {
"total_fare": "360.00",
"tax": "104.00"
},
"restrictions": {
"refundable": false,
"change_penalties": true
}
}
}
]
}
And I use the code below to retrieve the value from the Json. I able to retrieve the "departs_at", "arrives_at", "marketing_airline" but I unable to retrieve the value inside "booking_info".
IOperations _obj = ClsOperations.GetOperations();
string url = "https://api.sandbox.amadeus.com/v1.2/flights/low-fare-search?apikey=" + APIKEY
+ "&origin=" + origin + "&destination=" + destination
+ "&departure_date=" + departureDate + "&return_date=" + returnDate
+ "¤cy=" + currency + "&number_of_results=1";
string json = _obj.GetJsonResult(url);
JToken jToken = JToken.Parse(json);
JArray outBoundFlights = (JArray)jToken.SelectToken("results[0].itineraries[0].outbound.flights");
foreach (JToken obf in outBoundFlights)
{
TravelPlan.Text += "Departs At: " + obf["departs_at"] + "<br/>";
TravelPlan.Text += "Arrives At: " + obf["arrives_at"] + "<br/>";
TravelPlan.Text += "Airline: " + obf["marketing_airline"] + "<br/>";
}
JArray outBoundFlightsBooking = (JArray)jToken.SelectToken("results[0].itineraries[0].outbound.flights.booking_info");
foreach (JToken obfb in outBoundFlightsBooking)
{
TravelPlan.Text += "<br/>";
TravelPlan.Text += "Travel Class: " + obfb["travel_class"] + "<br/>";
TravelPlan.Text += "Seats Remaining: " + obfb["seats_remaining"] + "<br/>";
}
I wish to ask how possible I can retrieve the value inside the booking_info? Thanks for every members here that help.
public class Origin
{
public string airport { get; set; }
}
public class Destination
{
public string airport { get; set; }
public string terminal { get; set; }
}
public class BookingInfo
{
public string travel_class { get; set; }
public string booking_code { get; set; }
public int seats_remaining { get; set; }
}
public class Flight
{
public string departs_at { get; set; }
public string arrives_at { get; set; }
public Origin origin { get; set; }
public Destination destination { get; set; }
public string marketing_airline { get; set; }
public string operating_airline { get; set; }
public string flight_number { get; set; }
public string aircraft { get; set; }
public BookingInfo booking_info { get; set; }
}
public class Outbound
{
public List<Flight> flights { get; set; }
}
public class Origin2
{
public string airport { get; set; }
public string terminal { get; set; }
}
public class Destination2
{
public string airport { get; set; }
}
public class BookingInfo2
{
public string travel_class { get; set; }
public string booking_code { get; set; }
public int seats_remaining { get; set; }
}
public class Flight2
{
public string departs_at { get; set; }
public string arrives_at { get; set; }
public Origin2 origin { get; set; }
public Destination2 destination { get; set; }
public string marketing_airline { get; set; }
public string operating_airline { get; set; }
public string flight_number { get; set; }
public string aircraft { get; set; }
public BookingInfo2 booking_info { get; set; }
}
public class Inbound
{
public List<Flight2> flights { get; set; }
}
public class Itinerary
{
public Outbound outbound { get; set; }
public Inbound inbound { get; set; }
}
public class PricePerAdult
{
public string total_fare { get; set; }
public string tax { get; set; }
}
public class Restrictions
{
public bool refundable { get; set; }
public bool change_penalties { get; set; }
}
public class Fare
{
public string total_price { get; set; }
public PricePerAdult price_per_adult { get; set; }
public Restrictions restrictions { get; set; }
}
public class Result
{
public List<Itinerary> itineraries { get; set; }
public Fare fare { get; set; }
}
public class RootObject
{
public string currency { get; set; }
public List<Result> results { get; set; }
}
This will work like charm.
string json = "{\"currency\": \"MYR\", \"results\": [ { \"itineraries\": [ { \"outbound\": { \"flights\": [ { \"departs_at\": \"2018-06-03T06:25\", \"arrives_at\": \"2018-06-03T07:25\", \"origin\": { \"airport\": \"PEN\" }, \"destination\": { \"airport\": \"KUL\", \"terminal\": \"M\" }, \"marketing_airline\": \"OD\", \"operating_airline\": \"OD\", \"flight_number\": \"2105\", \"aircraft\": \"738\", \"booking_info\": { \"travel_class\": \"ECONOMY\", \"booking_code\": \"Q\", \"seats_remaining\": 9 } } ] }, \"inbound\": { \"flights\": [ { \"departs_at\": \"2018-06-04T14:10\", \"arrives_at\": \"2018-06-04T15:10\", \"origin\": { \"airport\": \"KUL\", \"terminal\": \"M\" }, \"destination\": { \"airport\": \"PEN\" }, \"marketing_airline\": \"OD\", \"operating_airline\": \"OD\", \"flight_number\": \"2108\", \"aircraft\": \"739\", \"booking_info\": { \"travel_class\": \"ECONOMY\", \"booking_code\": \"O\", \"seats_remaining\": 5 } } ] } } ], \"fare\": { \"total_price\": \"360.00\", \"price_per_adult\": { \"total_fare\": \"360.00\", \"tax\": \"104.00\" }, \"restrictions\": { \"refundable\": false, \"change_penalties\": true } } } ]}";
RootObject travelInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json);
For Retrieving all bookingInfo in the json response.
foreach(Result result in travelInfo.results)
{
foreach(Itinerary itn in travelInfo.results[0].itineraries)
{
//For Inbound Flights
foreach (Flight2 fl2 in itn.inbound.flights)
{
Console.WriteLine("BookingCode:"+fl2.booking_info.booking_code);
Console.WriteLine("Seats Remaining:" + fl2.booking_info.seats_remaining);
Console.WriteLine("Travel Class:" + fl2.booking_info.travel_class);
}
//For Outbound Flights
foreach(Flight fl1 in itn.outbound.flights)
{
Console.WriteLine("BookingCode:" + fl1.booking_info.booking_code);
Console.WriteLine("Seats Remaining:" + fl1.booking_info.seats_remaining);
Console.WriteLine("Travel Class:" + fl1.booking_info.travel_class);
}
}
}
Output:
BookingCode:O Seats Remaining:5 Travel Class:ECONOMY BookingCode:Q Seats Remaining:9 Travel Class:ECONOMY