I´m need to parse a json object into a c# class, my problem is that json object has a nested array and it´s throwing some errors when parsing.
I have tried a couple of options: a) do foreach in the elements of nested array, and add them to a new array b) parsing using json.deserialize
No success so far
These are my c# classes
public class itemPrediccion
{
public string ClavePartido { get; set; }
public string Ganador { get; set; }
public bool EsFavorito { get; set; }
}
public class Prediccion
{
public ObjectId _id { get; set; }
public string IdUsuario { get; set; }
public int Jornada { get; set; }
public IEnumerable<itemPrediccion> PrediccionesJornada { get; set; }
}
An object of class "Prediccion" would contain a list of "itemPrediccion"
This is the json object that I want to parse to a "Prediccion" object
{
"IdUsuario" : "user1",
"Jornada" : "1",
"PrediccionesJornada" : [
{
"ClavePartido" : "AP2019J1P1",
"Ganador": "Morelia",
"EsFavorito": "false"
},
{
"ClavePartido" : "AP2019J1P2",
"Ganador": "Chivas",
"EsFavorito": "false"
},
{
"ClavePartido" : "AP2019J1P3",
"Ganador": "Atlas",
"EsFavorito": "true"
}
]
}
This is how I´m trying to deserialize
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
Prediccion prediccionUsuario = new Prediccion {
IdUsuario = data.IdUsuario,
Jornada = data.Jornada,
PrediccionesJornada = data.PrediccionesJornada
};
Throws this error:
Cannot implicitly convert type 'Newtonsoft.Json.Linq.JArray' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?)