C
C#•11mo ago
agaitan026

issues loading winform with api rest

hi i got the following code:
using Json.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net.Http;
using Syncfusion.WinForms.DataGrid;

namespace SfDataGrid_REST_API
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
sfDataGrid1.DataSource = GetRESTData("http://45.237.184.122:4000/clientes?limit=10");
this.sfDataGrid1.Columns.Add(new GridTextColumn() { MappingName = "OrderID" });
this.sfDataGrid1.Columns.Add(new GridTextColumn() { MappingName = "CustomerID" });
this.sfDataGrid1.Columns.Add(new GridTextColumn() { MappingName = "EmployeeID" });
this.sfDataGrid1.Columns.Add(new GridTextColumn() { MappingName = "Freight" });
this.sfDataGrid1.Columns.Add(new GridTextColumn() { MappingName = "ShipCity" });
this.sfDataGrid1.Columns.Add(new GridTextColumn() { MappingName = "Verified" });
}

private JArray GetRESTData(string uri)
{
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
var webResponse = (HttpWebResponse)webRequest.GetResponse();
var reader = new StreamReader(webResponse.GetResponseStream());
string data = reader.ReadToEnd();
return JsonConvert.DeserializeObject<JArray>(data);
}
}
}
using Json.Net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Net.Http;
using Syncfusion.WinForms.DataGrid;

namespace SfDataGrid_REST_API
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
sfDataGrid1.DataSource = GetRESTData("http://45.237.184.122:4000/clientes?limit=10");
this.sfDataGrid1.Columns.Add(new GridTextColumn() { MappingName = "OrderID" });
this.sfDataGrid1.Columns.Add(new GridTextColumn() { MappingName = "CustomerID" });
this.sfDataGrid1.Columns.Add(new GridTextColumn() { MappingName = "EmployeeID" });
this.sfDataGrid1.Columns.Add(new GridTextColumn() { MappingName = "Freight" });
this.sfDataGrid1.Columns.Add(new GridTextColumn() { MappingName = "ShipCity" });
this.sfDataGrid1.Columns.Add(new GridTextColumn() { MappingName = "Verified" });
}

private JArray GetRESTData(string uri)
{
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
var webResponse = (HttpWebResponse)webRequest.GetResponse();
var reader = new StreamReader(webResponse.GetResponseStream());
string data = reader.ReadToEnd();
return JsonConvert.DeserializeObject<JArray>(data);
}
}
}
but getting this: Newtonsoft.Json.JsonSerializationException: 'Deserialized JSON type 'Newtonsoft.Json.Linq.JObject' is not compatible with expected type 'Newtonsoft.Json.Linq.JArray'. Path '', line 1, position 9636.' what im missing?
8 Replies
Angius
Angius•11mo ago
OOF reason you're rawdogging it with JObjects and JArrays instead of proper classes? Any reason you're using the deprecated WebRequest instead of HttpClient? The error just tells you that the response contains a single object, but you're trying to get an array out of it So, a quick fix would be using JObject instead of JArray But it'd be putting lipstick on a pig
agaitan026
agaitan026OP•11mo ago
any reason i should use httpclient? is faster?
Angius
Angius•11mo ago
It's not deprecated
agaitan026
agaitan026OP•11mo ago
was a sample provided 😦 just learning
Angius
Angius•11mo ago
Well it's a bad sample then, just gonna say straight away
agaitan026
agaitan026OP•11mo ago
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
sfDataGrid1.DataSource = GetRESTData("http://45.237.184.122:4000/clientes?limit=10");
this.sfDataGrid1.Columns.Add(new GridTextColumn() { MappingName = "idclave_nombre" });
}

private JObject GetRESTData(string uri)
{
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
var webResponse = (HttpWebResponse)webRequest.GetResponse();
var reader = new StreamReader(webResponse.GetResponseStream());
string data = reader.ReadToEnd();
return JsonConvert.DeserializeObject<JObject>(data);

}
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
sfDataGrid1.DataSource = GetRESTData("http://45.237.184.122:4000/clientes?limit=10");
this.sfDataGrid1.Columns.Add(new GridTextColumn() { MappingName = "idclave_nombre" });
}

private JObject GetRESTData(string uri)
{
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
var webResponse = (HttpWebResponse)webRequest.GetResponse();
var reader = new StreamReader(webResponse.GetResponseStream());
string data = reader.ReadToEnd();
return JsonConvert.DeserializeObject<JObject>(data);

}
}
}
i see i should redo all with httpclient then
Angius
Angius•11mo ago
Getting data from an API should be something more akin to
private static HttpClient _client = new();

private async Task<SomeDataClass> GetData(string url)
{
return await _client.GetFromJsonAsync<SomeDataClass>(url);
}
private static HttpClient _client = new();

private async Task<SomeDataClass> GetData(string url)
{
return await _client.GetFromJsonAsync<SomeDataClass>(url);
}
With SomeDataClass being a class that describes your JSON Which you can even generate, if need be $jsongen
MODiX
MODiX•11mo 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.
Want results from more Discord servers?
Add your server