I'm quite new to Json files and Blazor, and I was trying to deserialize a Json string, but when I try to fill a list with objects obtained via deserializer the list is full of null objects.
I'm also using Flurl
, I don't know if it's of any relevance, but I'll just mention it to be sure.
The string I'm deserializing is this one :
[
{
"id": 1,
"description": "DASHBOARD",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
},
{
"id": 6,
"description": "Accounting",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
},
{
"id": 7,
"description": "Anti-avoidance obligations",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
},
{
"id": 8,
"description": "Personal data",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
},
{
"id": 9,
"description": "Date",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
},
{
"id": 10,
"description": "Balance",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
},
{
"id": 11,
"description": "Communication",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
},
{
"id": 12,
"description": "F24 - F23 and communications",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
},
{
"id": 13,
"description": "Management",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
},
{
"id": 14,
"description": "StudyOK",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
},
{
"id": 15,
"description": "730",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
},
{
"id": 16,
"description": "770",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
},
{
"id": 17,
"description": "Fees",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
},
{
"id": 18,
"description": "Certification",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
},
{
"id": 19,
"description": "IMU",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
},
{
"id": 20,
"description": "ISA",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
},
{
"id": 21,
"description": "IVA",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
},
{
"id": 22,
"description": "PF",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
},
{
"id": 23,
"description": "SC",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
},
{
"id": 24,
"description": "SP",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
},
{
"id": 25,
"description": "Widget",
"position": 0,
"enabled": true,
"itemSections": [],
"users": []
}
]
This is the code :
var tokenTrimmed = token2.Split("\"token\":\"")[1].Split('"')[0].Trim();
result = await url.WithOAuthBearerToken(tokenTrimmed).GetStringAsync();
var jsonDocument = JsonDocument.Parse(result);
Console.WriteLine(jsonDocument.RootElement.ToString());
items = JsonSerializer.Deserialize<List<GridList>>(jsonDocument.RootElement);
This is the GridList Class :
@code {
public int Id { get; set; }
public string Description { get; set; }
public int Position { get; set; }
public bool Enabled { get; set; }
public List<string> ItemSections { get; set; }
public List<string> Users { get; set; }
}
And this is what I get:
So to summarize:
var options = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
});
var result = JsonSerializer.Deserialize<List<GridList>>(yourJsonAsString, options);