I have a strange json, which:
- I can not change it, it is from a third party
- It can have up to 75 properties
A simple example of the json:
{
"bookingcode":["ABC","DEF", "GHJ", "TEST"],
"referencenumber":[123, 456]
"bookingdate":["22-07-2022T14:00:30", "23-11-2022T17:00:25"]
}
I am only interested in the last value of the array and want to deserialize into a class called Booking, so in this case bookingcode="TEST"
and bookingdate="23-11-2022T17:00:25"
.
A solution would be define a model:
public class Booking
{
public string BookingCode { get; set; }
public int ReferenceNumber { get; set; }
public DateTime BookingDate { get; set;}
}
public class BookingInput
{
public List<string> BookingCode { get; set;}
public List<int> ReferenceNumber { get; set; }
public List<DateTime> BookingDate { get; set; }
}
var bookinginput = JsonSerializer.Deserialize<BookingInput>(jsonString);
var booking = new Booking
{
BookingCode = bookinginput.BookingCode.Last(),
ReferenceNumber = bookinginput.ReferenceNumber.Last(),
...
}
I think it is very tedious to write out all 75 properties in code like this. Is there a better solution in Json.net or System.Text.Json for this?