我正在尝试使用 .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; }
}
}