View JSON data in Listview

Asked

Viewed 307 times

1

Guys as I load a Listview with the result of a method that returns an object already loaded with JSON?

Method that loads object with JSON

namespace MeuProjeto.ViewModels.jsonModels
{
    class JSON
    {
        /*async void getJSON<T>(object sender, String URL, System.EventArgs e)*/
        public async Task<T> getJSON<T>(String URL, Boolean Complemento, Boolean UtilizarExemplo)
        {
            var response = @"{'avisos':[
                              {
                                ""id"": 1,
                                ""titulo"": ""blá blá"",
                                ""descricao"": ""Hoje terá comida"",
                                ""data_inicial"": ""28/10/2017"",
                                ""data_final"": ""28/10/2017""
                              },
                              {
                                ""id"": 2,
                                ""titulo"": ""Atraso"",
                                ""descricao"": ""Esta semana irei atrasar 10minutos"",
                                ""data_inicial"": ""30/10/2017"",
                                ""data_final"": ""03/11/2017""
                              }
                             ]}";

            var client = new HttpClient();
            /*var dados = JsonConvert.DeserializeObject<UsuarioList>(response); */
            if (UtilizarExemplo)
            { 
                T dados = JsonConvert.DeserializeObject<T>(response);
                return dados;
            }
            else
            {
                var json = await client.GetStringAsync(URL);
                if (Complemento)
                    json = "{" + Complemento + ":" + json + "}";

                T dados = JsonConvert.DeserializeObject<T>(json);
                return dados;
            }
        }
    }

How I call the method

await json.getJSON<ListaAviso>("", true, true);

And my model Listview

namespace MeuProjeto.ViewModels
{
    public class ViewList : ViewModel
    {
        public ICommand LoadCommand { get; set; }
        public ObservableCollection<Item> Items { get; set; }

        private int _index = 0;

        public virtual void Load(Boolean lCarregarVarios = true) { }

        public ViewList()
        {
            this.Items = new ObservableCollection<Item>();
            LoadCommand = new Command(() => Load(true));
            Load(false);
        }
    }
    public class Item : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        string id;
        public string ID
        {
            get { return id; }
            set
            {
                text = value;
                this.Notify("ID");
            }
        }

        string descricao;
        public string Descricao
        {
            get { return descricao; }
            set
            {
                detail = value;
                this.Notify("Descricao");
            }
        }

       [...] Demais campos [...]


        public override string ToString() { return this.Text; }

        private void Notify(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

1 answer

2


So far everything I’ve done with Listview has been class-based, setting the Bindinds to the corresponding property name. Usually working with json dice I play the json dice in a class and then feed the Listview with a List coming from this class.

In the code below is an example of what I usually do with data like json, I hope it helps:

//Transforma uma string json em uma lista objetos
    public List<Agencia> DeserializarListaObj(string json)
    {
        try
        {
            List<Agencia> sv = new List<Agencia>();

            sv = JsonConvert.DeserializeObject<List<Agencia>>(json);

            return sv;
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
    }

    //Transforma uma string json em um objeto
    public Agencia DeserializarObj(string json)
    {
        try
        {
            Agencia sv = new Agencia();

            sv = JsonConvert.DeserializeObject<Agencia>(json);

            return sv;
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
    }

    //Transforma uma lista de objetos em uma string json
    public string SerializarListaObj(List<Agencia> listObj)
    {
        string json = string.Empty;

        json = JsonConvert.SerializeObject(listObj);

        return json;
    }

    //Transforma um objeto em uma string json
    public string SerializarObj(Agencia obj)
    {
        string json = string.Empty;

        json = JsonConvert.SerializeObject(obj);

        return json;
    }
  • Thank you so much for the help little guy helped a lot, seeing your example I managed to apply in what I need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.