System.Text.Json 反序列化并获得 属性 个值

System.Text.Json Deserialize and get Property Values

我正在努力了解如何从使用 Visual Studio 'Paste Special' 派生的 class 访问 属性 值到新的 C# 模型中。 JSON 是从 API returning 日志事件复制的,在示例中我只包含一个日志事件,但 API 通常会 return 多个事件..

下面是 JSON 来自 API:

{
  "lastReadEventId": "event-1a29db8ad2c608d8079f040000000000",
  "scannedEventCount": 1,
  "eventEntities": [
    {
      "timestamp": "2021-02-16T21:59:15.7233546+00:00",
      "properties": [
        {
          "name": "LogEventCategory",
          "value": "System"
        },
        {
          "name": "LogEventType",
          "value": "Application Startup"
        },
        {
          "name": "LogEventSource",
          "value": "WebApp_RAZOR"
        },
        {
          "name": "LogUserId",
          "value": ""
        },
        {
          "name": "LogUsername",
          "value": ""
        },
        {
          "name": "LogForename",
          "value": ""
        },
        {
          "name": "LogSurname",
          "value": ""
        },
        {
          "name": "LogData",
          "value": "Application Starting Up"
        },
        {
          "name": "MachineName",
          "value": "DESKTOP-OS52032"
        },
        {
          "name": "ProcessId",
          "value": 16716
        },
        {
          "name": "ThreadId",
          "value": 1
        },
        {
          "name": "ApplicationSource",
          "value": "WebApp-RAZOR"
        }
      ],
      "messageTemplateTokens": [
        {
          "propertyName": "LogEventCategory",
          "rawText": "{@LogEventCategory}"
        },
        {
          "propertyName": "LogEventType",
          "rawText": "{@LogEventType}"
        },
        {
          "propertyName": "LogEventSource",
          "rawText": "{@LogEventSource}"
        },
        {
          "propertyName": "LogUserId",
          "rawText": "{@LogUserId}"
        },
        {
          "propertyName": "LogUsername",
          "rawText": "{@LogUsername}"
        },
        {
          "propertyName": "LogForename",
          "rawText": "{@LogForename}"
        },
        {
          "propertyName": "LogSurname",
          "rawText": "{@LogSurname}"
        },
        {
          "propertyName": "LogData",
          "rawText": "{@LogData}"
        }
      ],
      "eventType": "$A970522D",
      "level": "Information",
      "renderedMessage": "SystemApplication StartupWebApp_RAZORApplication Starting Up",
      "id": "event-1a29db8ad2c608d8079f040000000000",
      "links": {
        "Self": "api/events/event-1a29db8ad2c608d8079f040000000000{?download,render,clef}",
        "Group": "api/events/resources"
      }
    }
  ]
}

下面是使用特殊粘贴控件创建的模型:

public class SeqLogEvents
{
    public class Rootobject
    {
        public string LastReadEventId { get; set; }
        public int ScannedEventCount { get; set; }
        public Evententity[] EventEntities { get; set; }
    }

    public class Evententity
    {
        public DateTime Timestamp { get; set; }
        public Property1[] Properties { get; set; }
        public Messagetemplatetoken[] MessageTemplateTokens { get; set; }
        public string EventType { get; set; }
        public string Level { get; set; }
        public string TenderedMessage { get; set; }
        public string Id { get; set; }
        public Links Links { get; set; }
    }

    public class Links
    {
        public string Self { get; set; }
        public string Group { get; set; }
    }

    public class Property1
    {
        public string Name { get; set; }
        public object Value { get; set; }
    }

    public class Messagetemplatetoken
    {
        public string PropertyName { get; set; }
        public string RawText { get; set; }
    }

}

我需要能够访问每个部分中的每个 属性 值,包括嵌套项以及在 foreach 循环中迭代上述项的列表。我过去使用 Newtonsoft Json 成功地做到了这一点,但我正在尝试使用 system.text.json 作为较新的标准来实现相同的目标。

我什至无法访问第一个 属性 值。我试图按照此处的 MS 说明进行操作: How to read JSON as .NET objects (deserialize)

我的愚蠢错误,意识到我试图引用错误 class。

反序列化时应该引用 Rootobject: