Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

JSON to ObservableCollection to be used on ListView

$
0
0

I'm having problems to convert JSON data from an API to use on my ListView.

Drawn Class

public class Drawn
{
public string date { get; set; }
public string numbers { get; set; }
public string stars { get; set; }
}

public class RootObject
{
public List drawns { get; set; }
}

API Class
public class APIReader
{
ObservableCollection drawns;
public APIReader() {
this.drawns = new ObservableCollection();
}

    public ObservableCollection<Drawn> getDrawns()
    {
        return drawns;
    }

    public  async void getLastDrawnASync()
    {
        var client = new System.Net.Http.HttpClient();
        client.BaseAddress = new Uri("https://nunofcguerreiro.com/");
        var response = await client.GetAsync("api-euromillions-json");
        var drawnsJson = response.Content.ReadAsStringAsync().Result;
        var rootobject =  JsonConvert.DeserializeObject<RootObject>(drawnsJson);
        foreach (var drawn_json in rootobject.drawns)
        {
            this.drawns.Add(drawn_json);
        }
    }
}

On debuging I get "Drawn" objets in the rootobject.drawns list , but they are not being added to my ObservableCollection. I haven't figured out yet the problem. Im currently new at C# and Xamarin, would be nice to get some help from you guys.

Thanks in advance


Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>