skip to Main Content

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


  1. I don’t think that JSON is a good one, anyway:

    Private Sub Main()
        Dim myData = JsonConvert.DeserializeAnonymousType(s, New With {
            .DicomModalities = New With {
                .Modality1 = New With {
                    .AET = "",
                    .Port = 0,
                    .Host = "",
                    .AllowEcho = False,
                    .AllowFind = False,
                    .AllowMove = False,
                    .AllowStore = False
                },
                .Modality2 = New With {
                    .AET = "",
                    .Port = 0,
                    .Host = "",
                    .AllowEcho = False,
                    .AllowFind = False,
                    .AllowMove = False,
                    .AllowStore = False
                }
            }
        })
        Console.WriteLine($"Name1: Modality1, Value1: {myData.DicomModalities.Modality1.AET}")
        Console.WriteLine($"Name2: Modality2, Value2: {myData.DicomModalities.Modality2.AET}")
    End Sub
    
    Shared ReadOnly s 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}
          }
    }"
    

    Better, maybe you would want to create classes and deserialize with that class:

    Sub Main
        Dim myData = JsonConvert.DeserializeObject(Of Root)(s)
        Console.WriteLine($"Name1: Modality1, Value1: {myData.DicomModalities.Modality1.Aet}")
        Console.WriteLine($"Name2: Modality2, Value2: {myData.DicomModalities.Modality2.Aet}")
    End Sub
    
    Shared ReadOnly s 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}
          }
    }"
    
    Public Partial Class Root
        <JsonProperty("DicomModalities")>
        Public Property DicomModalities As DicomModalities
    End Class
    
    Public Partial Class DicomModalities
        <JsonProperty("Modality1")>
        Public Property Modality1 As Modality
        <JsonProperty("Modality2")>
        Public Property Modality2 As Modality
    End Class
    
    Public Partial Class Modality
        <JsonProperty("AET")>
        Public Property Aet As String
        <JsonProperty("Port")>
        Public Property Port As Long
        <JsonProperty("Host")>
        Public Property Host As String
        <JsonProperty("AllowEcho")>
        Public Property AllowEcho As Boolean
        <JsonProperty("AllowFind")>
        Public Property AllowFind As Boolean
        <JsonProperty("AllowMove")>
        Public Property AllowMove As Boolean
        <JsonProperty("AllowStore")>
        Public Property AllowStore As Boolean
    End 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):

    Sub Main()
        Dim myData = JsonConvert.DeserializeObject(Of Dictionary(Of String, Dictionary(Of String, Dictionary(Of String, Object))))(s)
        Dim modalities = myData("DicomModalities")
    
        For Each modality In modalities
            Console.WriteLine($"Name: {modality.Key}, Value: {modality.Value("AET")}")
        Next
    End Sub
    

    Dotnet Fiddle demo

    Login or Signup to reply.
  2. your code is working in my VS 2022, but you can try this syntax

           Dim list As New List(Of String)
    
            For Each modality As JProperty In dicomModalities.Properties()
               list.Add( modality.Name & "-" & modality.Value.SelectToken("AET").Value(Of String) & "-" & modality.Value.SelectToken("Host").Value(Of String) )
            Next
    
            Console.WriteLine(String.Join(vbCrLf, list))
    

    output

    Modality1-PACS1-192.168.253.5
    Modality2-GINKGO-127.0.0.1
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search