JsonPropertyAttribute 不适用于 .名义上

JsonPropertyAttribute not working with . in name

我正在使用 ASP.NET Core 3.1 为工作项创建触发器创建 Azure DevOps webhook 端点。 DevOps 产生的有效载荷有 . Fields 数组

上某些 属性 个名称中的字符
"System.AreaPath": "FabrikamCloud",
"System.TeamProject": "FabrikamCloud",
"System.IterationPath": "FabrikamCloud\Release 1\Sprint 1",
"System.WorkItemType": "Bug",
"System.State": "New",
"System.Reason": "New defect reported",
"System.CreatedDate": "2014-07-15T17:42:44.663Z",
"System.CreatedBy": {

我已经创建了模型来表示对象图的各个级别并且父对象序列化得很好,但即使我适当地注释了这些属性,它们也不会反序列化并且所有值都设置为默认值

public class Fields
{
    [JsonProperty("System.AreaPath")]
    public string SystemAreaPath { get; set; }

    [JsonProperty("System.TeamProject")]
    public string SystemTeamProject { get; set; }

    [JsonProperty("System.IterationPath")]
    public string SystemIterationPath { get; set; }

    [JsonProperty("System.WorkItemType")]
    public string SystemWorkItemType { get; set; }

    [JsonProperty("System.State")]
    public string SystemState { get; set; }

    [JsonProperty("System.Reason")]
    public string SystemReason { get; set; }

    [JsonProperty("System.CreatedDate")]
    public DateTime SystemCreatedDate { get; set; }

    [JsonProperty("System.CreatedBy")]
    public UserDetails SystemCreatedBy { get; set; }

    [JsonProperty("System.ChangedDate")]
    public DateTime SystemChangedDate { get; set; }

    [JsonProperty("System.ChangedBy")]
    public UserDetails SystemChangedBy { get; set; }

    [JsonProperty("System.Title")]
    public string SystemTitle { get; set; }

有人知道如何处理 属性 包含小数点的名称吗?

只要您使用 Newtonsoft Json 来处理 JSON serialization/deserialization,Json属性 属性就可以正常工作。但是AspNet Core 3+默认使用System.Text.Json.

这是一篇配置它以使用 Newtonsoft 的文章:

https://dotnetcoretutorials.com/2019/12/19/using-newtonsoft-json-in-net-core-3-projects/

您可以使用

[JsonPropertyName("System.AreaPath")]

而不是

[JsonProperty("System.AreaPath")] 

来自System.Text.Json,如@Hintee

所述,默认情况下在.net core 3中使用