I’m trying to iterate through all "DicomModalities" in the following json string and get data in the following format but my code isnt working and gives the error "’name’ is not a member of ‘JObject’"
required format >>> (dicommodality name)-AET eg for the first entry it would be "Modality1-PACS1" and for the second it would be "Modality2-GINKGO"
Json:
{
"DicomModalities" : {
"Modality1" : {"AET" : "PACS1",
"Port" : 4242,
"Host" : "192.168.253.5",
"AllowEcho" : true,
"AllowFind" : false,
"AllowMove" : true,
"AllowStore" : true},
"Modality2" : {"AET" : "GINKGO",
"Port" : 11112,
"Host" : "127.0.0.1",
"AllowEcho" : false,
"AllowFind" : false,
"AllowMove" : false,
"AllowStore" : true}
}
}
My Code
Imports Newtonsoft.Json.Linq
Sub Main()
Dim jsonString As String = "{ "DicomModalities" : { "Modality1" : {"AET" : "PACS1", "Port" : 4242, "Host" : "192.168.253.5", "AllowEcho" : true, "AllowFind" : false, "AllowMove" : true, "AllowStore" : true}, "Modality2" : {"AET" : "GINKGO", "Port" : 11112, "Host" : "127.0.0.1", "AllowEcho" : false, "AllowFind" : false, "AllowMove" : false, "AllowStore" : true} } }"
Dim json As JObject = JObject.Parse(jsonString)
Dim dicomModalities As JObject = json("DicomModalities")
Dim message As String = ""
For Each modality As JProperty In dicomModalities.Properties()
message = modality.Name & "-" & modality.Value("AET").ToString()
MessageBox.Show(message)
Next
End Sub
2
Answers
I don’t think that JSON is a good one, anyway:
Better, maybe you would want to create classes and deserialize with that class:
EDIT: Knowing it would always be this depth and structure, but not knowing names like "modularity1", "modularity2" you could deserialize into nested Dictionaries (better would be to make that an array if you had control on JSON generated):
Dotnet Fiddle demo
your code is working in my VS 2022, but you can try this syntax
output