Xamarin C # does not deserialize json

0

I am trying to convert some parameters that I receive in a json in whole, but for some strange reason I do not get it, can anyone know that I am failing ???

switch (response.StatusCod) {
  case (System.Net.HttpStatusCode.OK):
    res_Label_api.Text = "good";

    var responseString = await response.Content.ReadAsStringAsync();



    // var xjson = JsonConvert.DeserializeObject(responseString);
    var xjson = JsonConvert.DeserializeObject < DatosEnvio > (responseString); // hasta aquí  que recibe todos los datos del json
    var xjson_id = xjson.IdUsuario; // pero una vez aquí me dice que le paso un parametro null D:
    int xid = Convert.ToInt32(xjson_id);
}

//modelo

public class DatosEnvio
{
    public string Usuario { get; set; }
    public string IdUsuario { get; set; }
    public string Nombre { get; set; }
}

public class RootObject
{
    public DatosEnvio DatosEnvio { get; set; }
    public object DatosEnvioJson { get; set; }
    public object DatosEnvioJsonDatos { get; set; }
    public object DatosEnvioJsonTitulos { get; set; }
    public object tabla { get; set; }
    public object tablas { get; set; }
    public string bandera { get; set; }
    public string mensaje { get; set; }
}

//json
{
    "DatosEnvio": {
        "Usuario": "prueba",
        "IdUsuario": "1",
        "Nombre": "Desarrollo Aige"
    },
    "DatosEnvioJson": null,
    "DatosEnvioJsonDatos": null,
    "DatosEnvioJsonTitulos": null,
    "tabla": null,
    "tablas": null,
    "bandera": "0",
    "mensaje": "Acceso exitoso"
}
    
asked by E.Rawrdríguez.Ophanim 21.02.2018 в 20:57
source

1 answer

2

You are badly applying deserialization since the correct type is different from the one you apply. You must deserialize to type RootObject and then do

var xjson = JsonConvert.DeserializeObject <RootObject>(responseString);
var xjson_id = xjson.DatosEnvio. IdUsuario;
    
answered by 21.02.2018 / 22:22
source