英文:
Rest API call Request returning null
问题
使用 RestSharp,想要对此 API 进行简单调用:https://dbd.tricky.lol/api/characters
具体来说,我希望将其中一个角色作为对象在我的 C# 程序中使用。以下是调用:
var options = new RestClientOptions("https://dbd.tricky.lol/api");
var client = new RestClient(options);
var response = await client.GetJsonAsync<Killers>("characters?id=Chuckles");
以下是我想要反序列化的模型:
public int _id { get; set; }
public string name { get; set; }
public string gender { get; set; }
public string tunables { get; set; }
public string difficulty { get; set; }
public string height { get; set; }
响应总是为空。我做错了什么?
英文:
Using RestSharp, want to make a simple call to this API: https://dbd.tricky.lol/api/characters
Specifically, I want one of the characters as an object in my C# Program. Here's the call:
var options = new RestClientOptions("https://dbd.tricky.lol/api");
var client = new RestClient(options);
var response = await client.GetJsonAsync<Killers>("characters?id=Chuckles");
And Here is the Model I want to deserialize to:
public int _id { get; set; }
public string name { get; set; }
public string gender { get; set; }
public string tunables { get; set; }
public string difficulty { get; set; }
public string height { get; set; }
The response always ends up being null. What am I doing wrong?
答案1
得分: 2
你需要实际阅读API文档并调用正确的URL。另外,你的模型是错误的。以下是你的代码的正确版本:
var options = new RestClientOptions("https://dbd.tricky.lol/api");
var client = new RestClient(options);
var response = await client.GetJsonAsync<Killer>("characterinfo?character=Chuckles");
public class Killer
{
public int index { get; set; }
public string id { get; set; }
public string alias {get;set;}
public string gender { get; set; }
public string difficulty { get; set; }
public string height { get; set; }
public Tunables tunables {get;set;}
}
public class Tunables
{
public int maxwalkspeed {get;set;}
public int terrorradius {get;set;}
public int basebeartrapcarrycapacity {get;set;}
}
这将产生以下结果:
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/pyDx5.png
我没有添加所有的类变量,你可以自行添加,但这完成了你上面问题的请求。
英文:
You need to actually read the API doc and call the right URL. also your model is incorrect. Here is the correct version of your code
var options = new RestClientOptions("https://dbd.tricky.lol/api");
var client = new RestClient(options);
var response = await client.GetJsonAsync<Killer>("characterinfo?character=Chuckles");
public class Killer
{
public int index { get; set; }
public string id { get; set; }
public string alias {get;set;}
public string gender { get; set; }
public string difficulty { get; set; }
public string height { get; set; }
public Tunables tunables {get;set;}
}
public class Tunables
{
public int maxwalkspeed {get;set;}
public int terrorradius {get;set;}
public int basebeartrapcarrycapacity {get;set;}
}
This produces the following :
I didn't add all the class variables for this as you can go through and do that, but this completes the request from your question above.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论