C# - 如何从 Azure DevOps 工作项列表反序列化 json?

问题描述

致力于从 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); 

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...