无法使用Newtonsoft Json.net从JSON字符串中获取所需值。

huangapple go评论73阅读模式
英文:

Unable to get required values from json string using Newtonsoft Json.net

问题

Sure, here's the translated 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

This code should help you iterate through the "DicomModalities" in your JSON and get the data in the required format.

英文:

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

答案1

得分: 0

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

英文:

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

答案2

得分: 0

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
英文:

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

huangapple
  • 本文由 发表于 2023年5月20日 22:08:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定