我是C#API的新手...我使用nodejs服务器创建了一个新服务器,并从SQL Server获取数据以生成json,并且我正在使用RestSharp使用c#Windows应用程序创建自定义应用程序,并将其发送到服务器并收到请求使用Newtonsoft的响应,我收到错误,无法反序列化当前json数组,这有助于我解决以下代码问题
JSON
[[{"id":2000,"engine":1,"wheel":1,"ac":1,"nitro":1,"rim":1},{"id":2001,"engine":1,"wheel":1,"ac":1,"nitro":1,"rim":1}]]
代码
using RestSharp;
using Newtonsoft.Json;
private void Form1_Load(object sender, EventArgs e)
{
var client = new RestClient("http://localhost:8000/employees");
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
var x = JsonConvert.DeserializeObject<RootObject>(response.Content);
foreach (var player in x.Abbrecipes)
{
comboBox1.Items.Add(player.id);
}
}
public class Abbrecipe
{
public int id { get; set; }
public int engine { get; set; }
public int wheel { get; set; }
public int ac { get; set; }
public int nitro { get; set; }
public int rim { get; set; }
}
public class RootObject
{
public List<Abbrecipe> Abbrecipes { get; set; }
}
答案 0 :(得分:0)
以下是使用WebClient
而非RestClient
的方法的当前情况代码:
private static Rootobject publicFeed;
public static void Set()
{
publicFeed = new Rootobject();
using (var client = new WebClient())
{
string result;
try
{
result = client.DownloadString("your json");//configure json stringdata
}
catch (Exception)
{
Console.WriteLine("This application needs a valid internet connection, please try again.");
result = null;
return;
}
publicFeed = JsonConvert.DeserializeObject<Rootobject>(result);
}
}