using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace jsondataexample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            getdataAsync();




        }

        public class Address
        {
            public string Street { get; set; }
            public string City { get; set; }
            public string Zipcode { get; set; }
        }

        public class User
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Email { get; set; }
            public Address Address { get; set; }  // Nested Address object

        }




        private async Task getdataAsync()
        {

            string url = "https://jsonplaceholder.typicode.com/users";  // Example URL



            try
            {
                // Create HttpClient instance
                using (HttpClient client = new HttpClient())
                {
                    // Send GET request to the URL
                    HttpResponseMessage response = await client.GetAsync(url);

                    // Ensure the response is successful (status code 200)
                    response.EnsureSuccessStatusCode();

                    // Read the JSON data from the response as a string
                    string json = await response.Content.ReadAsStringAsync();

                    // Deserialize JSON string to a List of User objects
                    List users = JsonConvert.DeserializeObject>(json);

                    // Print each user's info
                    foreach (var user in users)
                    {
                        Console.WriteLine($"Id: {user.Id}, Name: {user.Name}, Email: {user.Email}, Street: {user.Address.Street}");
                    }
                }
            }
            catch (Exception ex)
            {
                // Handle errors (e.g., network issues, invalid URL)
                Console.WriteLine("An error occurred: " + ex.Message);
            }

        }


    }
}