I’m trying to deserialize this string to put the data in a table (I’m using Blazor), I only need the ItemSections’ ItemId, the rest of it can be disposed of. Thing is I’m trying to make it work but I’m a bit stuck.
This is the string :
"id": 6,
"description": "Accounting",
"position": 0,
"enabled": true,
"itemSections": [
{
"itemId": 7,
"sectionId": 6,
"item": {
"id": 7,
"type": 2,
"description": "ctb_1",
"notes": "",
"shortDescription": "ctb_1",
"code": "string",
"enabled": true,
"guid": null,
"itemSections": [
null
],
"layouts": [],
"scopes": [],
"users": [],
"bookmarks": [],
"tenantId": null
},
"section": null,
"position": 0
}
I tried using these classes but it doesn’t work, I’m kind of new to the deserializing so I just tried, fully knowing that the chances of it working were very low.
The classes :
public class GridList
{
public int Id { get; set; }
public string Description { get; set; }
public int Position { get; set; }
public bool Enabled { get; set; }
public List<ItemSection> ItemSections { get; set; }
public string Users { get; set; }
}
public class ItemSection
{
public int ItemId { get; set; }
}
This is the deserializing, just in case it’s useful (items is a List) :
items = JsonSerializer.Deserialize<List<GridList>>(jsonDocument.RootElement, new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
});
Also the table I’m trying where I’m trying to put all of the data, maybe there is something wrong here too :
<table class="table table-striped">
<thead>
<tr>
@* headers *@
<th>Id</th>
<th>Description</th>
<th>Position</th>
<th>Enabled</th>
<th>Item Sections</th>
<th>Users</th>
<th>Data</th>
</tr>
</thead>
<tbody>
@foreach (var item in items)
{
@* dati *@
<tr>
<td>@item.Id</td>
<td>@item.Description</td>
<td>@item.Position</td>
<td>@item.Enabled</td>
<td>@string.Join(", ", item.ItemSections)</td>
<td>@string.Join(", ", item.Users)</td>
<td>
<button class="btn btn-secondary" @onclick="(e => GetId(item.Id)) style="background: linear-gradient(to bottom, #0b2363, #3a0647);">Apri Pagina</button>
</td>
</tr>
}
</tbody>
</table>
Michelle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.