[Solved] Foreach on Json based on value [closed]


Here is a crude example, that uses the Newtonsoft.Json nuget package, you can work from and use as inspiration perhaps. It groups by table name and then gets the list for each one. You may want to do it differently I’m not sure.

var o = JsonConvert.DeserializeObject<root>(json);
var groups = o.tabla.GroupBy(t => t.nombretabla);

foreach (var group in groups)
{
    var tableName = group.Key;

    var tabla = group.FirstOrDefault();
    if (tabla != null)
    {
        var list = tabla.campostabla.campo.ToList();

        foreach (var item in list)
        {
            Console.WriteLine(item);
        }
    }
}

Boiler plate classes that you should change as required…

public class root
{
    public Tabla[] tabla { get; set; }
}

public class Tabla
{
    public string nombretabla { get; set; }
    public CamposTabla campostabla { get; set; }
    public string filtro { get; set; }
}

public class CamposTabla
{
    public string[] campo { get; set; }
}

0

solved Foreach on Json based on value [closed]