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
Angius11mo 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
RagequitMasterOP11mo ago
So what's data's type here ?
Angius
Angius11mo 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
MODiX11mo 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
RagequitMasterOP11mo ago
So I have these classes
No description
RagequitMaster
RagequitMasterOP11mo ago
No description
RagequitMaster
RagequitMasterOP11mo ago
Then I do this ... I'll get { "ID" = "1", "Name" = "Johnson", "Adress" = "New York", "Phone" = "12345678", "ClassID" = "101", "Portrait " = "Uglyface.png" } ?
Angius
Angius11mo ago
Sinhvien instead of Root, I assume? And... is that data incorrect?
RagequitMaster
RagequitMasterOP11mo ago
After getting that, how do I show it on the Datagrid ?
Angius
Angius11mo 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
RagequitMasterOP11mo ago
Hey I'm reading this And I wonder what's that
var result
var result
No description
RagequitMaster
RagequitMasterOP11mo ago
Like the type of the variable ?
Angius
Angius11mo ago
You mean var? It's type inference
RagequitMaster
RagequitMasterOP11mo ago
I know what's var is but that line
var result = ...
var result = ...
What's the use of it ?
Angius
Angius11mo 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
RagequitMasterOP11mo 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
Angius11mo 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
RagequitMasterOP11mo ago
Oh ... Okay Thanks
RagequitMaster
RagequitMasterOP11mo ago
Hey um what are these lines for ?
No description
Angius
Angius11mo 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
RagequitMasterOP11mo 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