首页 > 解决方案 > 将类映射到 unirest 对象并显示响应数据

问题描述

我正在尝试使用 .NET 显示和映射对 Unirest 类的响应。我在堆栈上遵循了这个示例:Convert the http response body to JSON format using Unirest C#

但是,在运行它时,我收到一个错误“无法将'System.IO.MemoryStream'类型的对象转换为'Root Object'类型。”

我的代码:

protected void Button1_Click(object sender, EventArgs e)
{
    HttpResponse<JobSearchFeed.RootObject> jsonResponse = Unirest.get("http://api.indeed.com/ads/apisearch?publisher=PUBLISHER_KEY&v=2&l=California&q=javascript")
    .header("X-Mashape-Key", "MY_KEY")
    .header("Accept", "application/json")
    .asJson<JobSearchFeed.RootObject>();
}

我也环顾四周,发现我需要将它用作“任务”,但我不确定这是否甚至捕获任何数据。

我的代码:

static async Task<JobSearchFeed.RootObject> GetRootInfo()
{
   HttpResponse<JobSearchFeed.RootObject> jsonResponse = await Unirest.get("http://api.indeed.com/ads/apisearch?publisher=PUBLISHER_KEY&v=2&l=California&q=javascript")
    .header("X-Mashape-Key", "MY_KEY")
    .header("Accept", "application/json")
    .asJsonAsync<JobSearchFeed.RootObject>();
    return jsonResponse.Body;
}

我的课:

public class JobSearchFeed
{
     public class Result
        {
            public string jobtitle { get; set; }
            public string company { get; set; }
            public string city { get; set; }
            public string state { get; set; }
            public string country { get; set; }
            public string language { get; set; }
            public string formattedLocation { get; set; }
            public string source { get; set; }
            public string date { get; set; }
            public string snippet { get; set; }
            public string url { get; set; }
            public string onmousedown { get; set; }
            public string jobkey { get; set; }
            public bool sponsored { get; set; }
            public bool expired { get; set; }
            public bool indeedApply { get; set; }
            public string formattedLocationFull { get; set; }
            public string formattedRelativeTime { get; set; }
            public string stations { get; set; }
        }

        public class RootObject
        {
            public int version { get; set; }
            public string query { get; set; }
            public string location { get; set; }
            public string paginationPayload { get; set; }
            public int radius { get; set; }
            public bool dupefilter { get; set; }
            public bool highlight { get; set; }
            public int totalResults { get; set; }
            public int start { get; set; }
            public int end { get; set; }
            public int pageNumber { get; set; }
            public List<Result> results { get; set; }
        }
}

标签: .netunirestmashape

解决方案


答案很困难,因为该项目不容易跟进。让我按照解决方法:

  1. NuGet 包相当老了,它针对的是旧的 NET Framework 4.0 - NET Framework 与 4.0 相比几乎没有变化。我不知道您使用的是哪个目标,但我确定是较新的目标。检查https://www.nuget.org/packages/Unirest-API/了解所有规范。
  2. 如果我们检查源项目有3个,很难说哪一个是正确的:https ://github.com/zeeshanejaz/unirest-net , https://github.com/apimatic/unirest-nethttps://github.com/Kong/unirest-net。因为nuget的作者我怀疑是第一个,但是Project Site指向了第二个的githubraw。
  3. 当我们使用 pdb 调试原始程序集时,源代码无法绑定到关键路径,这意味着有问题。
  4. 如果我们将源代码从第二个复制到项目中,它就可以工作

我建议如下:

  1. 将库的源代码复制到您的项目中 - 库很小,直到 2015 年才更新,所以它应该不是一个大问题
  2. 不要使用不受支持的库,而是自己使用 HttpClient 和 JsonConvert 来完成。对你来说完全一样,你跳过这个问题

有问题的行 - 不知何故,代码进入了这种情况并试图严重地投射对象:

        else if (typeof (Stream).GetType().IsAssignableFrom(typeof (T).GetType()))
        {
          this.Body = (T) this.Raw;
        }

推荐阅读