Display JSON data in ListView

1

How do 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));
        }
    }
}
    
asked by anonymous 28.10.2017 / 19:48

1 answer

2

So far all I've done with ListView was based on classes, setting the Bindinds to the corresponding property name. Generally working with json data I play the json data 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 json-type data, 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;
    }
    
29.10.2017 / 20:32