I have this simple class for mapping JSON to C#:
Public Class AtributeRaport
Public Property NumeRaport As String
Public Property ApplicationId As String
Public Property ObjectId As String
Public Property Selections As String
Public Property Deprecated As String
End Class
Public Class Rapoarte
Public Property Id As String
Public Property AtributeRaport As List(Of AtributeRaport)
End Class
I have this Sub, so far, for adding objects to the JSON file:
Sub AddToJson(id As String, nume As String, appId As String, objectId As String, selections As String, deprecated As String)
Dim filePath = System.AppDomain.CurrentDomain.BaseDirectory & "\reports.json"
Dim jsonData = IO.File.ReadAllText(filePath)
Dim jsonroot As List(Of Rapoarte) = If(JsonConvert.DeserializeObject(Of List(Of Rapoarte))(jsonData), New List(Of Rapoarte))
Dim attrlist = New AtributeRaport With {
.ApplicationId = appId,
.NumeRaport = nume,
.ObjectId = objectId,
.Selections = selections,
.Deprecated = deprecated
}
jsonroot.Add(New Rapoarte With {.Id = id, .AtributeRaport = New List(Of AtributeRaport)})
Dim serializerSettings As New JsonSerializerSettings
serializerSettings.NullValueHandling = NullValueHandling.Ignore
serializerSettings.Formatting = Formatting.Indented
jsonData = JsonConvert.SerializeObject(jsonroot, serializerSettings)
IO.File.WriteAllText(filePath, jsonData)
End Sub
If I run this, the output is:
[
{
"Id": "1",
"AtributeRaport": []
}
]
For the life of me, I don't know how to add those pesky values which I declare in my attrlist.
Also, if I want to get all the values under a certain id, from JSON, would this approach work:
Public Function GetAttributesJson(id As String, filepath As String) As List(Of String)
Dim jObject As JObject = JObject.Load(New JsonTextReader(File.OpenText(filepath)))
Dim attrlist As List(Of String) = New List(Of String)
Dim resources As JArray = CType(jObject("id"), JArray)
For Each attr In resources.Where(Function(obj) obj("id").Value(Of String) Is id.ToString)
attrlist.Add(attr("ApplicationId").Value(Of String))
attrlist.Add(attr("NumeRaport").Value(Of String))
attrlist.Add(attr("ObjectId").Value(Of String))
attrlist.Add(attr("Selections").Value(Of String))
attrlist.Add(attr("Deprecated").Value(Of String))
Next
Return attrlist
End Function
Thank you so much for any hint!