我正在使用 mashape api 获取速度发布跟踪信息:-
https://www.mashape.com/blaazetech/indian-post
因为这是在 .NET c# 中,所以以下代码未被编译:-
Task<HttpResponse<MyClass>> response = Unirest.get("https://indianpost.p.mashape.com/index.php?itemno=EF990403084IN")
.header("X-Mashape-Key", mykey)
.header("Accept", "application/json")
.asJson();
编译错误是“无法从用法中推断方法‘unirest_net.request.HttpRequest.asJson()’的类型参数。尝试明确指定类型参数。”
我不确定如何使用此 api。 “MyClass”有问题吗?
最佳答案
RSDC - 好的,事实证明您的 Indian-Post API 端点无论如何都不起作用。在 Mashape 上测试它们并返回错误。
>>> 我让它在 metaCritic GET API 上工作 <<<
https://www.mashape.com/byroredux/metacritic (游戏列表 API,第二个向下)
回复:MyClass
1) 在 API 文档页面的 mashape.com 站点上,找到右侧的 200/JSON 响应。
2) 复制json数据
3) 转到 http://json2csharp.com/并粘贴代码
4) 点击Generate按钮获取c#类代码。复制类(class)代码。
5) 返回 VS,转到 Models 文件夹并创建名为 MyClass.cs 的类。
6) 按如下方式粘贴您的代码:
public class MyClass
{
public class Result
{
public string name { get; set; }
public string score { get; set; }
public string url { get; set; }
public string rlsdate { get; set; }
public string rating { get; set; }
public string summary { get; set; }
public string platform { get; set; }
}
public class RootObject
{
public List<Result> results { get; set; }
}
}
7) 试试这个:
HttpResponse<MyClass.RootObject> response = Unirest.get("https://byroredux-metacritic.p.mashape.com/game-list/ps4/coming-soon")
.header("X-Mashape-Key", "KxdVFN6Vlymshd5ezOQwBvS2Svjtp1bq5YOjsnFOkgTOwqwM6y")
.header("Accept", "application/json")
.asJson<MyClass.RootObject>();
如果运行调试器,您可以看到 response > Body > results 现在包含 25 项数据。
关于c# - 将 mashape api 响应转换为 c# 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28936933/