c# - 将 mashape api 响应转换为 c# 类

标签 c# .net mashape

我正在使用 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/

相关文章:

.net - Resharper dotSettings 文件每次都被修改

c# - 我应该如何比较两个列表中的值?

c# - 使用 C# 和 Regex 查找并包围某些 html 文本中的所有单词和数字

c# - Visual Studio 绿色警告下划线

.net - MSMQ 在 Azure 上可用

ios - 使用 camfind 快速上传图片请求

java - 如何解析来自 Unirest 调用的 JSON 结果

json - 如何在 Swift 上通过 Mashape 打印 JSON 数据?

c# - .Net 2.0 Windows 服务在垃圾回收期间挂起

c# - Entity Framework 和添加 POCO,而不添加子对象