可以使用Json2csharp.com网站之类的东西来代替手动建模。粘贴在示例JSON响应中,越饱满越好,然后提取生成的生成类。至少,这消除了一些活动部分,将使您获得csharp中JSON的形状,从而使序列化程序更轻松,而且您不必添加属性。
使其工作,然后对您的类名进行修改,以符合您的命名约定,并在以后添加属性。
编辑:好了,经过一番混乱,我已经成功地将结果反序列化为作业列表(我使用Json2csharp.com为我创建了类)
public class Job{ public string id { get; set; } public string position_title { get; set; } public string organization_name { get; set; } public string rate_interval_pre { get; set; } public int minimum { get; set; } public int maximum { get; set; } public string start_date { get; set; } public string end_date { get; set; } public List<string> locations { get; set; } public string url { get; set; }}然后对您的代码进行编辑:
List<Job> model = null; var client = new HttpClient(); var task = client.GetAsync("http://api.usa.gov/jobs/search.json?query=nursing+jobs") .ContinueWith((taskwithresponse) => { var response = taskwithresponse.Result; var jsonString = response.Content.ReadAsStringAsync(); jsonString.Wait(); model = JsonConvert.DeserializeObject<List<Job>>(jsonString.Result); }); task.Wait();这意味着您可以摆脱包含的对象。值得注意的是,这不是与任务相关的问题,而是反序列化的问题。
编辑2:
有一种方法可以获取JSON对象并在Visual Studio中生成类。只需复制选择的JSON,然后编辑>选择性粘贴>将JSON粘贴为类。整个页面专门用于此:
http://blog.preinside.eu/2014/09/08/Visual-Studio-2013-Paste-Special-JSON-
And-Xml/



