I'm serializing Objects as JSON String in a .txt file with the following code:
private void ReserveTable_Click(object sender, EventArgs e)
{
Reservation CreateReservation = new Reservation(textBoxFirstName.Text, textBoxSecondName.Text, dateTimePickerDate.Value.ToShortDateString(), comboBoxTable.Text, 5, textBoxPhoneNumber.Text);
string jsonString = JsonSerializer.Serialize(CreateReservation);
File.AppendAllText(Globals.filepath, jsonString);
}
It creates multiple json String in that file, looking like this:
{"Firstname":"Rainer","Lastname":"Maier","Date":"07.07.2022","Table":"Tisch 8 (Au\u00DFenbereich)","NumberOfPersons":5,"Phonenumber":"4756465465"}{"Firstname":"Timo","Lastname":"Winkler","Date":"07.07.2022","Table":"Tisch 8 (Au\u00DFenbereich)","NumberOfPersons":5,"Phonenumber":"5674654"}
Then im trying to deserialize the file, to put the attributes into a table:
private void ShowReservation_Click(object sender, EventArgs e)
{
string jsonString = File.ReadAllText(Globals.filepath);
Reservation ReadReservation = JsonSerializer.Deserialize<Reservation>(jsonString)!;
Console.WriteLine(ReadReservation);
}
I'm not able to deserialize both objects. With the Error Code:
'{' is invalid after a single JSON value. Expected end of data.
Is there any way to deserialize the multiple Objects or to serialize it smarter? In this program you can create multiple reservations which need to be stored in a file.