C# HTTP Flurl ListView JSON Tutorial Example.

In this tutorial we see a practical example of how to download and parse JSON data from online and bind the data in a C# windows forms listview.

C# HTTP Flurl ListView

Video Tutorial

Here’s the video tutorial version of this class.

Demo

C# ListView HTTP FLurl

Program.cs

Here’s the full Program.cs file.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Flurl.Http;

namespace JSONListView
{ /*
     * User class. A POCO class.
     */
    class User
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
    }
    class Program
    {
        private static SynchronizationContext synchronizationContext;
        private static ListView myListView;
        private static Label progressLabel;
        /*
         * Setup ListView. Instantiate, then add columns then register the ItemSelectionChanged events
         */
        private static void setupListView()
        {
            myListView = new ListView { Location = new Point(30, 70), Size = new Size(400, 400), View = View.Details, FullRowSelect = true, Alignment = ListViewAlignment.SnapToGrid };
            myListView.Columns.Add("ID", 100);
            myListView.Columns.Add("NAME", 150);
            myListView.Columns.Add("EMAIL", 150);

            myListView.ItemSelectionChanged += myListView_ItemSelectionChanged;

            progressLabel = new Label()
            {
                Location = new Point(50, 50),
                ClientSize = new Size(200, 15),
                ForeColor = Color.AliceBlue,
                Text = "Downloading data.........Please wait"
            };
        }
        /*
         * Listen to ItemSelectionChange events for our ListView.
         * Then show the name in a messagebox
         */
        static void myListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            ListViewItem listViewItem = e.Item;
            String name = listViewItem.SubItems[1].Text;
            if (e.IsSelected)
            {
                MessageBox.Show(name,"ListView JSON",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
        }
        /*
         * Download json data from online using Http Flurl. Do it asynchronously.
         */
        private static async void downloadJSONData()
        {
            try
            {
                //PERFORM IN BACKGROUND
                await Task.Run(async () =>
                {
                    List<User> users = await "https://jsonplaceholder.typicode.com/users".GetJsonAsync<List<User>>();
                    foreach (User user in users)
                    {
                        synchronizationContext.Post(value =>
                        {
                            var mUser = (User)value;
                            progressLabel.Text = "Download Complete.";
                            myListView.Items.Add(new ListViewItem(new[] { mUser.Id, mUser.Name, mUser.Email }));

                        }, user);
                    }
                });
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        /*
     * Create a Form, Set its properties and run the application.
     */
        private static void createForm()
        {
            synchronizationContext = SynchronizationContext.Current;

            Form myForm = new Form
            {
                Text = "C# Flurl HTTP : ListView - Fill From JSON",
                ClientSize = new Size(564, 520),
                BackColor = Color.MediumSeaGreen
            };

            myForm.Controls.Add(progressLabel);
            myForm.Controls.Add(myListView);
            Application.EnableVisualStyles();
            Application.Run(myForm);
        }
        /*
        * Main method
        */
        public static void Main()
        {
            setupListView();
            downloadJSONData();
            createForm();
        }
    }
}