Search code examples
c#arraysjsonloopsdeserialization

Looping through JSON Array with multiple Objects and getting values c#


I've serialized an array of json String to a .txt file, which looks like this

[{
  "Firstname":"wdwed",
  "Lastname":"wedwed",
  "Date":"04.07.2022",
  "Table":"Tisch 5 (Innenbereich)",
  "NumberOfPersons":5,
  "Phonenumber":"727171"
},
{
  "Firstname":"Timo",
  "Lastname":"Winkler",
  "Date":"04.07.2022",
  "Table":"Tisch 7 (Au\u00DFenbereich)",
  "NumberOfPersons":5,
  "Phonenumber":"asdasd"
}]

date, string Table, int NumberOfPersons, string Phonenumber)
    {
        this.Firstname = firstname;
        this.Lastname = lastname;
        this.Date = date;
        this.Table = Table;
        this.NumberOfPersons = NumberOfPersons;
        this.Phonenumber = Phonenumber;
    }
}

Here is the Reservation class:

public class Reservation
{
    public string Firstname { get; set; }

    public string Lastname { get; set; }

    public string Date { get; set; }

    public string Table { get; set; }

    public int NumberOfPersons { get; set; }

    public string Phonenumber { get; set; }

   
    public Reservation(string firstname, string lastname, string 

Now I'm deserializing this .txt to a list.

private void ShowReservation_Click(object sender, EventArgs e)
{
    string jsonString = File.ReadAllText(Globals.filepath);
    var ReservationList = JsonSerializer.Deserialize<List<Reservation>>(jsonString);
    Console.WriteLine(ReservationList);    
}

How can I get each value of each JSON Object of this json Array?

I want to put the values into a table, created with WindowsForms.

   this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.ColumnFirstName,
        this.ColumnSecondName,
        this.ColumnDate,
        this.ColumnTable,
        this.ColumnNumberOfPersons,
        this.ColumnPhoneNumber});
        this.dataGridView1.Location = new System.Drawing.Point(25, 31);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.RowTemplate.Height = 25;
        this.dataGridView1.Size = new System.Drawing.Size(644, 265);
        this.dataGridView1.TabIndex = 7;

Solution

  • I dont know if you use c# WebForms but you can use the "GridView" component like this to show a table:

    private void ShowReservation_Click(object sender, EventArgs e)
        {
            string jsonString = File.ReadAllText(Globals.filepath);
            var ReservationList = JsonSerializer.Deserialize<List<Reservation>>(jsonString);
    
            GridView1.DataSource = ReservationList;
            GridView1.DataBind();
        }
    
    
    <asp:GridView 
            ID="GridView1" 
            runat="server" >
            <Columns>
                <asp:BoundField DataField="Firstname" HeaderText="Firstname">
                <asp:BoundField DataField="Lastname" HeaderText="Lastname">
                <asp:BoundField DataField="Date" HeaderText="Date">
                <asp:BoundField DataField="Table" HeaderText="Table">
                <asp:BoundField DataField="NumberOfPersons" HeaderText="NumberOfPersons">
                <asp:BoundField DataField="Phonenumber" HeaderText="Phonenumber">
            </Columns>
        </asp:GridView>