Web API to Data Grid View (Winform)

Hi guys, I'm trying to make a web api and throw the data in the thing in my data grid view in my winform project. But I was taught the old ancient way, please tell me the new way. (I'm having an exam soon T-T)
21 Replies
Angius
Angius6mo ago
using var client = new HttpClient();
var data = await client.GetAsJsonAsync<T>(url);
using var client = new HttpClient();
var data = await client.GetAsJsonAsync<T>(url);
where T is a class describing what the data you get looks like
RagequitMaster
RagequitMaster6mo ago
So what's data's type here ?
Angius
Angius6mo ago
Whatever describes the JSON you get from the API If you get, say,
{
"name": "Bob",
"age": 57,
"pets": [
{ "species": "cat", "age": "7"}
]
}
{
"name": "Bob",
"age": 57,
"pets": [
{ "species": "cat", "age": "7"}
]
}
you would use something like
enum PetSpecies {
Cat,
Dog,
Parrot
}
class Pet
{
public PetSpecies Species { get; init; }
public int Age { get; init; }
}
class Root
{
public string Name { get; init; }
public int Age { get; init; }
public List<Pet> Pets { get; init; }
}
enum PetSpecies {
Cat,
Dog,
Parrot
}
class Pet
{
public PetSpecies Species { get; init; }
public int Age { get; init; }
}
class Root
{
public string Name { get; init; }
public int Age { get; init; }
public List<Pet> Pets { get; init; }
}
And do
var data = await client.GetFromJsonAsync<Root>("https://api.com/people.json");
var data = await client.GetFromJsonAsync<Root>("https://api.com/people.json");
There are ways to generate classes from JSON, too, so that makes things easier $jsongen
MODiX
MODiX6mo ago
Use https://app.quicktype.io or https://json2csharp.com to generate classes from your JSON
Instantly parse JSON in any language | quicktype
Whether you're using C#, Swift, TypeScript, Go, C++ or other languages, quicktype generates models and helper code for quickly and safely reading JSON in your apps. Customize online with advanced options, or download a command-line tool.
Convert JSON to C# Classes Online - Json2CSharp Toolkit
Convert any JSON object to C# classes online. Json2CSharp is a free toolkit that will help you generate C# classes on the fly.
RagequitMaster
RagequitMaster6mo ago
So I have these classes
No description
RagequitMaster
RagequitMaster6mo ago
No description
RagequitMaster
RagequitMaster6mo ago
Then I do this ... I'll get { "ID" = "1", "Name" = "Johnson", "Adress" = "New York", "Phone" = "12345678", "ClassID" = "101", "Portrait " = "Uglyface.png" } ?
Angius
Angius6mo ago
Sinhvien instead of Root, I assume? And... is that data incorrect?
RagequitMaster
RagequitMaster6mo ago
After getting that, how do I show it on the Datagrid ?
Angius
Angius6mo ago
Create a DataSet, bind the Datagrid to it, add data to the DataSet https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datagrid?view=netframework-4.8.1#examples here's an example
RagequitMaster
RagequitMaster6mo ago
Hey I'm reading this And I wonder what's that
var result
var result
No description
RagequitMaster
RagequitMaster6mo ago
Like the type of the variable ?
Angius
Angius6mo ago
You mean var? It's type inference
RagequitMaster
RagequitMaster6mo ago
I know what's var is but that line
var result = ...
var result = ...
What's the use of it ?
Angius
Angius6mo ago
Since JsonConvert.DeserializeObject<List<JsonResult>>() returns List<JsonResult>, then var represents type List<JsonResult> The use is to get the result of deserialization? What do you mean? How else would you get the deserialized data from the method?
RagequitMaster
RagequitMaster6mo ago
I mean the result of deserialization, is it a string or a list or ... what ? That why I was confused with why use
var
var
instead of other things ? .-. Oh so its a list ?
Angius
Angius6mo ago
DeserializeObject is a generic method With a signature like
public T DeserializeObject<T>(string json)
public T DeserializeObject<T>(string json)
So whatever type you pass as T, you get out of it
RagequitMaster
RagequitMaster6mo ago
Oh ... Okay Thanks
RagequitMaster
RagequitMaster6mo ago
Hey um what are these lines for ?
No description
Angius
Angius6mo ago
Seems like they clear the request's accept headers, and add a new accept header telling the request to accept json data GetAaJsonAsync() already does that all
RagequitMaster
RagequitMaster6mo ago
Hey I've successfully called the API and show the data on the data grid Thanks for your help Now I gotta do Add, Delete and Edit the Json thingy
Want results from more Discord servers?
Add your server
More Posts
Windows 11 Dev Drive - file typesHey guys, anyone using Dev Drive on Windows ? From what I heard it supposed to used only for source ASP.NET Identity need help getting to respective logged in user's row in AspNetUsers tableHello all, What the title says, I need to know how to get to that row. Here's what I've tried alreadSTJ: Prevent or reverse escaping of base64 string during deserializationI am using STJ to decode a structure with a string member; the string contains an image in base64 foI would cherish if someone would share an idea on how to solve the Simple Algebra Problem.You are given a mathematical function f(x) and a value of a. Your task is to determine if the equatiproblem with entityframework migrationerror: An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing withoC# RoadmapI’ve been struggling for a while now on how to progress with learning C#. I’ve been given a lot of gDoes .NETZ MAUI rightly have the reputation of being half-baked?I'm a Swift guy, but I want to re-check on the Microsoft approach after my break up with Xamarin lik✅ Help me with TestI want to test RequestHandler (im using Mediator), but inside of that handler method I need to get uNeed help with a telephone directory Windows Forms applicationI am getting an exception when I try to click on a row to change the selected row in a DataGridView.How many unity exclusive functions and methods are there?Im talking about stuff like Navmesh agent, game.object and etc