title: stacked trying to deserialize a JSON response from an API with C#
body: <p>I'm building a C# app to get data from an web API.
  it connects to the web server... get data about stock markets, products, prices and money left in the account, etc. i need to individualize all that info so i can use it later to calculate best moments to get in and get out from the markets (buy and sell stocks).</p>
  
  <p>after that little  intro i'm stucked trying to deserialize the data.</p>
  
  <p>after one GET request to the API i get this JSON:</p>
  
  <pre><code>{
    "cuentas": [
      {
        "numero": "xxxx",
        "tipo": "aaaa",
        "moneda": "bbbb",
        "disponible": 407397.92,
        "comprometido": 0.00,
        "saldo": 407397.92,
        "titulosValorizados": 0.0,
        "total": 407397.92000,
        "margenDescubierto": 0.0,
        "saldos": [
          {
            "liquidacion": "inmediato",
            "saldo": 407397.92,
            "comprometido": 0.00,
            "disponible": 407397.92,
            "disponibleOperar": 407397.92
          },
          {
            "liquidacion": "hrs24",
            "saldo": 0.00,
            "comprometido": 0.00,
            "disponible": 0.00,
            "disponibleOperar": 407397.92
          },
          {
            "liquidacion": "hrs48",
            "saldo": 0.00,
            "comprometido": 0.00,
            "disponible": 0.00,
            "disponibleOperar": 407397.92
          },
          {
            "liquidacion": "hrs72",
            "saldo": 0.00,
            "comprometido": 0.00,
            "disponible": 0.00,
            "disponibleOperar": 407397.92
          },
          {
            "liquidacion": "masHrs72",
            "saldo": 0.00,
            "comprometido": 0.00,
            "disponible": 0.00,
            "disponibleOperar": 0.0
          }
        ],
        "estado": "operable"
      },
      {
        "numero": "zzzz",
        "tipo": "ccccc",
        "moneda": "ddddd",
        "disponible": 4574.25,
        "comprometido": 0.00,
        "saldo": 4574.25,
        "titulosValorizados": 0.0,
        "total": 4574.25,
        "margenDescubierto": 0.0,
        "saldos": [
          {
            "liquidacion": "inmediato",
            "saldo": 4574.25,
            "comprometido": 0.00,
            "disponible": 4574.25,
            "disponibleOperar": 4574.25
          },
          {
            "liquidacion": "hrs24",
            "saldo": 0.00,
            "comprometido": 0.00,
            "disponible": 0.00,
            "disponibleOperar": 4574.25
          },
          {
            "liquidacion": "hrs48",
            "saldo": 0.00,
            "comprometido": 0.00,
            "disponible": 0.00,
            "disponibleOperar": 4574.25
          },
          {
            "liquidacion": "hrs72",
            "saldo": 0.00,
            "comprometido": 0.00,
            "disponible": 0.00,
            "disponibleOperar": 4574.25
          },
          {
            "liquidacion": "masHrs72",
            "saldo": 0.00,
            "comprometido": 0.00,
            "disponible": 0.00,
            "disponibleOperar": 0.0
          }
        ],
        "estado": "operable"
      }
    ],
    "estadisticas": [
      {
        "descripcion": "Anterior",
        "cantidad": 27,
        "volumen": 1817447.95
      },
      {
        "descripcion": "Actual",
        "cantidad": 11,
        "volumen": 564217.96
      }
    ],
    "totalEnPesos": 699006.3575000000
  }
  </code></pre>
  
  <p>what i've done untill now:</p>
  
  <p>1) i've used a web app to convert the JSON data into the classes i'would need. and i've got this:</p>
  
  <pre><code>namespace QuickType
  {
      using System;
      using System.Collections.Generic;
  
      using System.Globalization;
      using Newtonsoft.Json;
      using Newtonsoft.Json.Converters;
  
      public partial class Lecturajson
      {
          [JsonProperty("cuentas")]
          public Cuenta[] Cuentas { get; set; }
  
          [JsonProperty("estadisticas")]
          public Estadistica[] Estadisticas { get; set; }
  
          [JsonProperty("totalEnPesos")]
          public double TotalEnPesos { get; set; }
      }
  
      public partial class Cuenta
      {
          [JsonProperty("numero")]
          public string Numero { get; set; }
  
          [JsonProperty("tipo")]
          public string Tipo { get; set; }
  
          [JsonProperty("moneda")]
          public string Moneda { get; set; }
  
          [JsonProperty("disponible")]
          public double Disponible { get; set; }
  
          [JsonProperty("comprometido")]
          public long Comprometido { get; set; }
  
          [JsonProperty("saldo")]
          public double Saldo { get; set; }
  
          [JsonProperty("titulosValorizados")]
          public long TitulosValorizados { get; set; }
  
          [JsonProperty("total")]
          public double Total { get; set; }
  
          [JsonProperty("margenDescubierto")]
          public long MargenDescubierto { get; set; }
  
          [JsonProperty("saldos")]
          public Saldo[] Saldos { get; set; }
  
          [JsonProperty("estado")]
          public string Estado { get; set; }
      }
  
      public partial class Saldo
      {
          [JsonProperty("liquidacion")]
          public string Liquidacion { get; set; }
  
          [JsonProperty("saldo")]
          public double SaldoSaldo { get; set; }
  
          [JsonProperty("comprometido")]
          public long Comprometido { get; set; }
  
          [JsonProperty("disponible")]
          public double Disponible { get; set; }
  
          [JsonProperty("disponibleOperar")]
          public double DisponibleOperar { get; set; }
      }
  
      public partial class Estadistica
      {
          [JsonProperty("descripcion")]
          public string Descripcion { get; set; }
  
          [JsonProperty("cantidad")]
          public long Cantidad { get; set; }
  
          [JsonProperty("volumen")]
          public double Volumen { get; set; }
      }
  }
  </code></pre>
  
  <p>2) i'm thinking in something like: (the string "raw" that gets the JSON its working ok)</p>
  
  <pre><code>IRestResponse responseestadocuenta = clientestadocuenta.Execute(requestestadocuenta);
  raw = Convert.ToString(responseestadocuenta.Content);
  var obj = JsonConvert.DeserializeObject&lt;Lecturajson&gt;(raw);
  </code></pre>
  
  <p>3) there is no 3. i'm stucked in "2".</p>
  
  <p>EXTRA PROBLEMS: this time i've got 2 accounts ("cuentas" in spanish if you look in the JSON. those are "numero": "xxxx" and "numero": "zzzz". But next time the user can get more accounts if he buys more... so the JSON response its fixed in his general structure but not in the number of "cuentas" ("numero" would be like the ID of each of them)</p>
  
  <hr>
  
  <p>HERE MY QUESTIONS:</p>
  
  <p>A) all this data will be refreshed each second or so... not sure yet and i think the best to deal with all this stuff is to store it into a SQL DB. do you think that's ok?? have in mind that this GET request is just 1 of like 10 or 15 other similar available in my app... just to picture the amount of info it will handle.</p>
  
  <p>B) I need some help with the deserealize in "2" so it can be compatible with my idea of store all of that in the SQL DB.</p>
  
  <p>******************************+</p>
  
  <p>I'm open to any help, correction, idea, new stuff to read. I've started with C# a few weeks ago and believe me i can't believe how far i've got until now. So forgive me if i'm asking newbie stuff.</p>
  
  <p>Thank you so much for your time and patience.</p>