我的第一个问题,所以请友善......:)
我正在使用C# HttpClient
调用作业 API 端点。
这是端点:Jobs API Endpoint(不需要密钥,点击即可) http://service.dice.com/api/rest/jobsearch/v1/simple.json?skill=ruby
这给了我这样的 JSON。
{
"count": 1117,
"firstDocument": 1,
"lastDocument": 50,
"nextUrl": "\/api\/rest\/jobsearch\/v1\/simple.json?areacode=&country=&state=&skill=ruby&city=&text=&ip=&diceid=&page=2",
"resultItemList": [
{
"detailUrl": "http:\/\/www.dice.com\/job\/result\/90887031\/918715?src=19",
"jobTitle": "Sr Security Engineer",
"company": "Accelon Inc",
"location": "San Francisco, CA",
"date": "2017-03-30"
},
{
"detailUrl": "http:\/\/www.dice.com\/job\/result\/cybercod\/BB7-13647094?src=19",
"jobTitle": "Platform Engineer - Ruby on Rails, AWS",
"company": "CyberCoders",
"location": "New York, NY",
"date": "2017-04-16"
}
]
}
我已经粘贴了完整的 JSON 片段,以便您可以在答案中使用它。完整的结果真的很期待这里。
这是 C# 类。
using Newtonsoft.Json;
using System.Collections.Generic;
namespace MyNameSpace
{
public class DiceApiJobWrapper
{
public int count { get; set; }
public int firstDocument { get; set; }
public int lastDocument { get; set; }
public string nextUrl { get; set; }
[JsonProperty("resultItemList")]
public List<DiceApiJob> DiceApiJobs { get; set; }
}
public class DiceApiJob
{
public string detailUrl { get; set; }
public string jobTitle { get; set; }
public string company { get; set; }
public string location { get; set; }
public string date { get; set; }
}
}
当我使用 HttpClient 调用 URL 并使用 JSON.NET 反序列化时,我确实正确地取回了数据。
这是我从控制台应用程序调用的代码Main
方法(因此static
列表,我认为这可以更好地重构?)
private static List<DiceApiJob> GetDiceJobs()
{
HttpClient httpClient = new HttpClient();
var jobs = new List<DiceApiJob>();
var task = httpClient.GetAsync("http://service.dice.com/api/rest/jobsearch/v1/simple.json?skill=ruby")
.ContinueWith((taskwithresponse) =>
{
var response = taskwithresponse.Result;
var jsonString = response.Content.ReadAsStringAsync();
jsonString.Wait();
var result = JsonConvert.DeserializeObject<DiceApiJobWrapper>(jsonString.Result);
if (result != null)
{
if (result.DiceApiJobs.Any())
jobs = result.DiceApiJobs.ToList();
if (result.nextUrl != null)
{
//
// do this GetDiceJobs again in a loop? How?? Any other efficient elegant way??
}
}
});
task.Wait();
return jobs;
}
但现在,我如何检查是否有更多工作使用nextUrl
场地?我知道我可以检查它是否不为空,如果不为空,则意味着还有更多工作需要取消。
我的调试和单步执行的结果 https://i.stack.imgur.com/N3sn7.png
如何以递归方式执行此操作,并且不会挂起且有一些延迟,这样我就不会超出 API 限制?我想我必须使用 TPL(任务并行库),但我很困惑。
谢谢你!
〜肖恩