【问题标题】:C# - How do I deserialize json from Azure DevOps Work Item List?C# - 如何从 Azure DevOps 工作项列表反序列化 json?
【发布时间】:2021-09-24 16:09:06
【问题描述】:

致力于从 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,
       "rev": 1,
       "fields": {
          "System.AreaPath": "Fabrikam-Fiber-Git",
          "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”又是如何处理的?

【问题讨论】:

  • Newtonsoft.Json 中,您可以将JsonProperty 属性添加到属性,使其看起来像public class FieldsModel { [JsonProperty("System.AreaPath")] public string AreaPath {get; set;} ...
  • 看起来有可用的包,其中包含您需要的模型:azure-devops

标签: c# api serialization azure-devops tfs-workitem


【解决方案1】:

您可以如下创建模型:

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); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 2011-05-16
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多