I am trying to display and map a response to a class from Unirest with .NET. I have followed this example on stack: Convert the http response body to JSON format using Unirest C#
However, when running it, I get an error "Unable to cast object of type 'System.IO.MemoryStream' to type 'Root Object'.'
My code:
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>();
}
I've also looked around and found that I need to use it as a "Task", but I'm not sure if this is even capturing any data at all.
My code for that:
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;
}
My class:
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; }
}
}