Please follow the below code. I tested for a project I am doing in VS2019 15.9.2 VB.net
My Json Data :
{
"Logs": [
{
"Time": "Mar 22 2020 4:17AM",
"Log": "New Alarm Logged"
},
{
"Time": "Mar 23 2020 7:37AM",
"Log": "Deleted automatically because Alarm is cleared."
}
]
}
We need to add Activity Logs with time stamps ( That's what I am trying
to do ), which is pretty similar with your application.
Class declaration as shown below
Imports Newtonsoft.Json
Public Class Log
Public Property Time As String
Public Property Log As String
End Class
Public Class ActivityLog
Public Property Logs As New List(Of Log)
End Class
Now we will make a function to collect the data to be entered into the Log array of json object.
Public Function _json_Fn_LogAppend(ByVal InJson As String, ByVal InValue As String) As String
_json_Fn_LogAppend = ""
Try
Dim _ActivityLog As ActivityLog = New ActivityLog
Dim _Log As Log = New Log
_Log.Time = Now().ToLongDateString
_Log.Log = InValue
_ActivityLog = JsonConvert.DeserializeObject(Of ActivityLog)(InJson)
_ActivityLog.Logs.Add(_Log)
_json_Fn_LogAppend = JsonConvert.SerializeObject(_ActivityLog, Formatting.Indented)
Console.WriteLine(_json_Fn_LogAppend)
Return _json_Fn_LogAppend
Catch ex As Exception
Console.WriteLine("ERROR/EX From Function <_json_Fn_LogAppend> : " + ex.ToString)
End Try
Return _json_Fn_LogAppend
End Function
Usage of Function where
Dim JsonDataCompiled As String = _json_Fn_LogAppend(Json, "Deleted automatically because Alarm is cleared.")
Json in the function is the existing json Data.