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?)

Dani AI

Generated

A quick diagnosis and a couple of practical fixes for the error you hit (thanks for the sample, ). The runtime error happens because when you deserialize into dynamic Json.NET returns a JArray for the JSON array — and a JArray cannot be implicitly assigned to IEnumerable<itemPrediccion>. The easiest, most robust fix is to deserialize directly into your concrete type so Json.NET maps the nested array for you:

var pred = JsonConvert.DeserializeObject<Prediccion>(requestBody);

This will handle nested arrays automatically when the JSON types match your C# properties. (newtonsoft.com)

If you need to work with the dynamic/JToken API (or want to transform values first), parse to a JObject/JArray and convert the array into your POCOs with ToObject<T>():

var j = JObject.Parse(requestBody);
var items = j["PrediccionesJornada"].ToObject<List<itemPrediccion>>();

That converts the JArray into a concrete list you can assign to PrediccionesJornada. (newtonsoft.com)

Two important data notes that will save debugging time: the posted JSON uses quoted values for numbers/booleans ("Jornada": "1", "EsFavorito": "false"). JSON booleans and numbers should be unquoted (for example Jornada: 1 and EsFavorito: false) — that avoids conversion surprises and makes automatic mapping reliable. If you cannot change the source JSON, parse those string values explicitly (use int.Parse/int.TryParse, bool.TryParse) before assigning. The JSON literals true/false are part of the JSON spec. (rfc-editor.org)

If was nudging you toward using an internal tool or team, that can help too — but for a quick code fix, prefer typed deserialization or use JToken.ToObject<T>() and explicit parsing of string-valued numbers/booleans. (newtonsoft.com)

I see you have development companies on your tagline. Why not use them?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.