我正在尝试使用.NET显示和映射来自Unirest的类的响应。我在堆栈上遵循了这个例子:Convert the http response body to JSON format using Unirest C#
然而,在运行它时,我收到错误“无法将类型为'System.IO.MemoryStream'的对象强制转换为'根对象'。'
我的代码:
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; }
}
}
答案 0 :(得分:0)
答案很困难,因为该项目不容易遵循。让我按照解决方法进行操作:
我建议以下内容:
存在问题的行-代码以某种方式进入此条件并尝试严重地投射对象:
else if (typeof (Stream).GetType().IsAssignableFrom(typeof (T).GetType()))
{
this.Body = (T) this.Raw;
}