问题描述
致力于从 Azure Devops 中提取工作项:
根据这篇文章,响应如下:https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/work%20items/list?view=azure-devops-rest-6.0
{
"count": 2,"value": [
{
"id": 297,"rev": 1,"fields": {
"System.AreaPath": "Fabrikam-Fiber-Git","System.Title": "Customer can sign in using their Microsoft Account",...
}
},{
"id": 298,"System.Title": "Customer can log out",...
}
}
]
}
我假设它的对象看起来像这样:
public class WorkItemmodel
{
public int id { get; set; }
public int rev { get; set; }
public FieldsModel fields { get; set; }
}
public class FieldsModel
{
public string AreaPath { get; set; }
public string Title { get; set; }
}
但是如何将其反序列化为正确的对象?以及如何处理“System.AreaPath”和“System.Title”?
解决方法
您可以创建如下模型:
public class FieldsModel
{
[JsonProperty("System.AreaPath")]
public string SystemAreaPath { get; set; }
[JsonProperty("System.Title")]
public string SystemTitle { get; set; }
}
public class WorkItemModel
{
public int id { get; set; }
public int rev { get; set; }
public FieldsModel fields { get; set; }
}
public class Root
{
public int count { get; set; }
public List<WorkItemModel> value { get; set; }
}
//using as below
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);