I have the following problem trying to deserialize JSON that I read from a file. I always get that it can't be deserialized by object type. What is causing this?
MyModel.cs
public class MyModel
{
public List<toppings> Lista { get; set; }
}
public class toppings
{
public string description { get; set; }
}
Program.cs
static void Main(string[] args)
{
try
{
string json = File.ReadAllText(@"C:\Users\Ricciardo\Documents\Net Core Practice\Pizza\PizzaRead\pizzas.json").ToString();
MyModel objetos = JsonSerializer.Deserialize<MyModel>(json);
foreach (var ingrediente in objetos.Lista)
{
Console.WriteLine(ingrediente.description.ToString());
}
}
catch (Exception ex)
{
Console.Write(ex.Message.ToString());
throw ex;
}
Console.ReadKey();
}
JSON File
[
{
"toppings": [
"pepperoni"
]
},
{
"toppings": [
"feta cheese"
]
},
{
"toppings": [
"pepperoni"
]
},
{
"toppings": [
"bacon"
]
}
]