On my server I do this:
[HttpGet]
public string GetSeleccionarTodasFilasTabla(string NombreTabla, string Version)
{
string resultado = string.Empty;
Engine.EngineDb FuncionDb = new Engine.EngineDb();
int permiso = FuncionDb.PermisoSync(Version);
if (permiso < 1)
{
return resultado = Engine.EngineData.SyncNoPermitida;
}
DataTable dt = new DataTable();
dt = FuncionDb.SeleccionarTodasFilasTabla(NombreTabla);
if (dt.Rows.Count != 0)
{
resultado = JsonConvert.SerializeObject(dt, Formatting.Indented);
}
else
{
resultado = Engine.EngineData.NoHayFilas;
}
return resultado;
}
It returns a string like this
"[\r\n {\r\n \"IdAbono\": 1,\r\n \"Nombre\": \"No abona\"\r\n },
\r\n {\r\n \"IdAbono\": 2,\r\n \"Nombre\": \"Abono orgánico\"\r\n },
\r\n {\r\n \"IdAbono\": 3,\r\n \"Nombre\": \"Fertilizante químico\"\r\n },
\r\n {\r\n \"IdAbono\": 4,\r\n \"Nombre\": \"Combinación\"\r\n }\r\n]"
In my application I have this:
private void ClientGetTablaAbono(string RequestURI)
{
string resultado = string.Empty;
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:52143/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = new HttpResponseMessage();
response = client.GetAsync(RequestURI).Result;
if (response.IsSuccessStatusCode)
{
resultado = response.Content.ReadAsStringAsync().Result;
List<Abono> TAbono = new List<Abono>();
//AQUI EL ERROR
TAbono = JsonConvert.DeserializeObject<List<Abono>>(resultado);
}
else
{
resultado = response.IsSuccessStatusCode.ToString();
richTextBox1.Text = resultado;
}
}
My Subscription Class is this:
public class Abono
{
public int IdAbono { get; set; }
public string Nombre { get; set; }
}
MISTAKE
Newtonsoft.Json.JsonSerializationException: 'Error converting value "[
{
"IdAbono": 1,
"Nombre": "No abona"
},
{
"IdAbono": 2,
"Nombre": "Abono orgánico"
},
{
"IdAbono": 3,
"Nombre": "Fertilizante químico"
},
{
"IdAbono": 4,
"Nombre": "Combinación"
}
]" to type 'System.Collections.Generic.List`1[ApiRestConsumer.Form2+Abono]'. Path '', line 1, position 308.'
ArgumentException: Could not cast or convert from System.String to System.Collections.Generic.List`1[ApiRestConsumer.Form2+Abono].
Apparently the issue is in the way the server is returning the data to you. My idea is that since you are serializing the datatable using the
JsonConvert
then the api may be performing a second serialization and therefore the json you are receiving is not exactly deserializable, at least not without first reversing the first serialization.Deserialize to string first.
In the line before deserializing the response, do a first deserialization to string.
What to do next? (If the first point works)
If the previous point works, it means that your server is sending the data not in the best possible way. I would recommend you make some adjustments but let's wait and see if this is it for good.
I don't know exactly what variant of web api you have (whether it's net core, or web api 2.x, or something else). But in any case the response from the api should be a json string and not a json string serialized to a string.
I hope it helps you.