I am building a Web Api in c # Where in one of the operations I return a string that serialized
like this:
public string GetCliente()
{
string resultado = string.Empty;
DataTable dt = new DataTable();
dt = Engine.FuncionesDb.TableDataCliente();
List<Cliente> Customer = new List<Cliente>();
if (dt.Rows.Count == 0)
{
return resultado;
}
else
{
Customer = SetListaCliente(dt);
resultado = JsonConvert.SerializeObject(Customer);
}
return resultado;
}
This method converts the DataTable into a List
private List<Cliente> SetListaCliente (DataTable dt)
{
List<Cliente> Customer = new List<Cliente>();
foreach (DataRow r in dt.Rows)
{
Cliente lineaCliente = new Cliente
{
Numero = Convert.ToInt32(r[0]),
Id = r[1].ToString(),
Nombre = r[2].ToString(),
Edad = Convert.ToInt32(r[3]),
Telefono = r[4].ToString(),
Mail = r[5].ToString(),
Saldo = Convert.ToDouble(r[6]),
FechaCreacion = Convert.ToDateTime(r[7]),
FechaCreacionUtc = Convert.ToDateTime(r[8]),
FechaModificacion = Convert.ToDateTime(r[9]),
FechaModificacionUtc = Convert.ToDateTime(r[10]),
Proceso = Convert.ToInt32(r[11]),
Usuario = r[12].ToString(),
Estado = r[13].ToString(),
};
Customer.Add(lineaCliente);
}
return Customer;
}
my client class
public class Cliente
{
public int Numero { get; set; }
public string Id { get; set; }
public string Nombre { get; set; }
public int Edad { get; set; }
public string Telefono { get; set; }
public string Mail { get; set; }
public double Saldo { get; set; }
public DateTime FechaCreacion { get; set; }
public DateTime FechaCreacionUtc { get; set; }
public DateTime FechaModificacion { get; set; }
public DateTime FechaModificacionUtc { get; set; }
public int Proceso { get; set; }
public string Usuario { get; set; }
public string Estado { get; set; }
}
How can I make a client list again in my client application? I have done it in several ways and it always returns error.
I should not give an error
resultado = response.Content.ReadAsStringAsync().Result;
List<Cliente> Customer = new JavaScriptSerializer().Deserialize<List<Cliente>>(resultado);