I'm struggling to understand how to access the property values from a class which was derived by using the Visual Studio 'Paste Special' into a new C# Model. The JSON was copied from an API returning log events, in the example I've only included one log event but the API will usually return multiple events..
Below is JSON from the 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"
}
}
]
}
Below is the model created from using the paste special control:
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; }
}
}
I need to be able to access every property value in each section, included nested items as well as iterating over a list of the above items in a foreach loop. I've done this successfully in the past using Newtonsoft Json but I'm trying to achieve the same using system.text.json as the newer standard.
I can't even get access the first property value. I was trying to follow the MS instructions here: How to read JSON as .NET objects (deserialize)