Search code examples
c#jsonapidatagrid

Trying to fill a Datagridview in C# with values from a JSON but i only get 1 cell fill


I'm working on an API that sends me this JSON:

{
  "cuentas": [
    {
      "numero": "1234",
      "tipo": "inversion",
      "moneda": "peso",
      "disponible": 12345,
      "comprometido": 0.00,
      "saldo": 12345,
      "titulosValorizados": 12345,
      "total": 12345,
      "margenDescubierto": 12345,
      "saldos": [
        {
          "liquidacion": "inmediato",
          "saldo": 12345,
          "comprometido": 0.00,
          "disponible": 123,
          "disponibleOperar": 123
        },
        {
          "liquidacion": "hrs24",
          "saldo": 0.00,
          "comprometido": 0.00,
          "disponible": 0.00,
          "disponibleOperar": 123
        },
        {
          "liquidacion": "hrs48",
          "saldo": 0.00,
          "comprometido": 0.00,
          "disponible": 0.00,
          "disponibleOperar": 123
        },
        {
          "liquidacion": "hrs72",
          "saldo": 0.00,
          "comprometido": 0.00,
          "disponible": 0.00,
          "disponibleOperar": 123
        },
        {
          "liquidacion": "masHrs72",
          "saldo": 0.00,
          "comprometido": 0.00,
          "disponible": 0.00,
          "disponibleOperar": 0.0
        }
      ],
      "estado": "operable"
    },
    {
      "numero": "123",
      "tipo": "inversion",
      "moneda": "dolar",
      "disponible": 0.68,
      "comprometido": 0.00,
      "saldo": 0.68,
      "titulosValorizados": 123,
      "total": 123,
      "margenDescubierto": 0.0,
      "saldos": [
        {
          "liquidacion": "inmediato",
          "saldo": 0.68,
          "comprometido": 0.00,
          "disponible": 0.68,
          "disponibleOperar": 0.68
        },
        {
          "liquidacion": "hrs24",
          "saldo": 0.00,
          "comprometido": 0.00,
          "disponible": 0.00,
          "disponibleOperar": 0.68
        },
        {
          "liquidacion": "hrs48",
          "saldo": 0.00,
          "comprometido": 0.00,
          "disponible": 0.00,
          "disponibleOperar": 0.68
        },
        {
          "liquidacion": "hrs72",
          "saldo": 0.00,
          "comprometido": 0.00,
          "disponible": 0.00,
          "disponibleOperar": 0.68
        },
        {
          "liquidacion": "masHrs72",
          "saldo": 0.00,
          "comprometido": 0.00,
          "disponible": 0.00,
          "disponibleOperar": 0.0
        }
      ],
      "estado": "operable"
    }
  ],
  "estadisticas": [
    {
      "descripcion": "Anterior",
      "cantidad": 123,
      "volumen": 123
    },
    {
      "descripcion": "Actual",
      "cantidad": 6,
      "volumen": 123.80
    }
  ],
  "totalEnPesos": 123.123
}

then i have these classes to work with:

public partial class LecturaJson
        {
            public Cuenta[] Cuentas { get; set; }
            public Estadistica[] Estadisticas { get; set; }
            public double TotalEnPesos { get; set; }
        }

        public partial class Cuenta
        {
            public long Numero { get; set; }
            public string Tipo { get; set; }
            public string Moneda { get; set; }
            public double Disponible { get; set; }
            public long Comprometido { get; set; }
            public double Saldo { get; set; }
            public double TitulosValorizados { get; set; }
            public double Total { get; set; }
            public long MargenDescubierto { get; set; }
            public Saldo[] Saldos { get; set; }
            public string Estado { get; set; }
        }

        public partial class Saldo
        {
            public string Liquidacion { get; set; }
            public double SaldoSaldo { get; set; }
            public long Comprometido { get; set; }
            public double Disponible { get; set; }
            public double DisponibleOperar { get; set; }
        }

        public partial class Estadistica
        {
            public string Descripcion { get; set; }
            public long Cantidad { get; set; }
            public double Volumen { get; set; }
        }

then i use this code to get the JSON

var response =GetResponseGET(sURL + "/api/v2/estadocuenta", bearer);

then i add [] to make it deserizable

response = "[" +response+ "]";

then i deserialize:

var s = JsonConvert.DeserializeObject<List<LecturaJson>>(response);

then try tu fill a datagridview:

dtgvedc.DataSource = s;

the problem is that de DATAGRID dtgedc (in the last code line) only gets 1 cell fullfiled... and nothing else...

Some of the values are new lists formed by new key-value groups

so its nos a plain dictionary.


Solution

  • The API result(LecturaJson) is the root class. The API result is not an array. The debugging helps to see what happens there. The array objects(Cuentas or Estadisticas ) can be set to the Datagridsources.

    This code changes the response to an array.

    response = "[" +response+ "]";

    If you delete the previous code, you can change it without "List".

    var s = JsonConvert.DeserializeObject<LecturaJson>(response);

    dtgvedc.DataSource = s.Lectures;
    dtgvedc2.DataSource = s.Cuentas;
    

    dtgvedc2 must be a new datagridview.