Home > Back-end >  Looping through JSON Array with multiple Objects and getting values c#
Looping through JSON Array with multiple Objects and getting values c#

Time:07-05

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;

CodePudding user response:

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>

CodePudding user response:

Presumably your ReservationList is populated, it's just not writing everything out like you want with Console.WriteLine(ReservationList)? It won't do that automatically, you need to tell it what you want to write. Without knowing exactly what Reservation looks like I'll guess that you could try something like this:

foreach(var reservation in ReservationList)
{
    var date = reservation.Date.ToString("dd/MM/yyyy");
    Console.WriteLine($"First Name: {reservation.Firstname}");
    Console.WriteLine($"Last Name: {reservation.Lastname}");
    Console.WriteLine($"Date: {date}");
    Console.WriteLine($"Table: {reservation.Table}");
    Console.WriteLine($"Number of Persons: {reservation.NumberOfPersons}");
    Console.WriteLine($"Phone Number: {reservation.PhoneNumber}");
}

CodePudding user response:

Here your JSON has a type of array you need access each object directly like that:

foreach(var obj in ReservationList){
Console.WriteLine(obj.Firstname  " // "  obj.Lastname ...);
}

CodePudding user response:

you don't need any classes. Just try this

DataTable table = JsonConvert.DeserializeObject<DataTable>(jsonString);
GridView1.DataSource = table;
GridView1.DataBind();
  • Related