Xamarin Json deserealization

0

When requesting my Api from my application, Api returns a very long JSON, of which only some data interests me, to process (deselate) my request, I have created a class with the help of the following page < a href="http://json2csharp.com/"> link , that helps me creating the relevant class for my json, what I want is to access my data and put them in a listview, but I think I'm doing something wrong. Especially my question is, once desealizado, how do I get to the objects of TABLE1 ???

Method

  HttpResponseMessage response = await client.SendAsync(request);


switch (response.StatusCode) {
  //200
  case (System.Net.HttpStatusCode.OK):

    HttpContent content = response.Content;
    string xjson = await content.ReadAsStringAsync();
    //string st_json = xjson.ToString();

    // 

    try {
      List < Table_Loc > loc_list = JsonConvert.DeserializeObject < List < Table_Loc >> (xjson);

      res_x.Text = xjson;
      /*
      mi sueño, mi epifanía sería algo asi ... creo??
      listloc es un listview
      ListoLoc.ItemsItemsSource =  List<Table_Loc> loc_list = JsonConvert.DeserializeObject<List<Table_Loc>>(xjson);
      */
      
      
    } catch (Exception ex) {
      await DisplayAlert("", "" + ex.ToString(), "ok");
      return;
    }

    break;

Json

{
    "DatosEnvio": null,
    "DatosEnvioJson": null,
    "DatosEnvioJsonDatos": null,
    "DatosEnvioJsonTitulos": null,
    "tabla": null,
    "tablas": {
        "Table1": [
            {
                "IdUsuario": 1,
                "Longitud": "-98.2491364",
                "Latitud": "19.0604784",
                "FechaAlta": "2018-02-22T11:58:33.7"
            }
        ]
    },
    "bandera": "0",
    "mensaje": "SE OBTUVIERON LOS DATOS DE MANERA CORRECTA"
}

Class

public class Table_Loc {
  public int IdUsuario {
    get;
    set;
  }
  public string Longitud {
    get;
    set;
  }
  public string Latitud {
    get;
    set;
  }
  public DateTime FechaAlta {
    get;
    set;
  }
}

public class Tablas {
  public List < Table_Loc > Table_Loc {
    get;
    set;
  }
}

public class Root {
  public object DatosEnvio {
    get;
    set;
  }
  public object DatosEnvioJson {
    get;
    set;
  }
  public object DatosEnvioJsonDatos {
    get;
    set;
  }
  public object DatosEnvioJsonTitulos {
    get;
    set;
  }
  public object tabla {
    get;
    set;
  }
  public Tablas tablas {
    get;
    set;
  }
  public string bandera {
    get;
    set;
  }
  public string mensaje {
    get;
    set;
  }
}
    
asked by E.Rawrdríguez.Ophanim 23.02.2018 в 01:09
source

1 answer

1

To properly deserialize the jsonresult that you have, you must take into account the structure with which you are going to do it, a good help is the page of link that correctly gives the model that you should use, but that for some reason you change the name of the Table1 to Table_Loc which in itself would not make you do the correct Deserialization

As a first step you must do the deserializacion using the object RootObject that gives you as Model the page (This object can be changed the name since it is not used as an internal list of your result) for this if you change the name to Root .

// Models Generados
public class Table1
{
    public int IdUsuario { get; set; }
    public string Longitud { get; set; }
    public string Latitud { get; set; }
    public DateTime FechaAlta { get; set; }
}

public class Tablas
{
    public List<Table1> Table1 { get; set; }
}

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

The logic you mention in the function is correct only that it should not be done using the Table_loc list but Root in this way:

Root miobject = JsonConvert.DeserializeObject<Root>(xjson);

After that you can access the properties of the variable miobject . It would be the try catch line like this:

try  
{
  Root myobject = JsonConvert.DeserializeObject<Root>(xjson);
  ListoLoc.ItemsItemsSource = myobject.tablas; //tablas es la lista de Table1 

} catch (Exception ex) {
  await DisplayAlert("", "" + ex.ToString(), "ok");
  return;
}

But the ItemsItemsSource property does not appear in the test I did of a Listview if you were kind enough to give me more information I would update the answer.

    
answered by 23.02.2018 / 18:10
source