我有这个 JSON:
{">JOE":{"id":7,"age":"23"},"BILLY":{"id":8,"age":"29"}}
我有这个解决方案用于 RajN 发布的更简单的 JSON 结构。
Dim j1 As String = "{ "JOE"":""0.90000000"",""JOE"":""3.30000000"",""MONROE"":""1.20000000""}"
Dim dict = JsonConvert.DeserializeObject(Of Dictionary(Of String, String))(j1)
For Each kvp In dict
Console.WriteLine(kvp.Key & " - " + kvp.Value)
Next
我正在寻找如何处理新的 JSON 数据。提前致谢
该
解决方案与您通过@RajN链接的其他解决方案几乎相同,只是您需要使用Dictionary(Of String, T)
而不是使用Dictionary(Of String, String)
其中T
是您定义的用于保存id
和age
的类。
因此,定义一个类:
Public Class PersonData
Public Property id As Integer
Public Property age As String
End Class
然后反序列化:
Dim json As String = "{""JOE"":{""id"":7,""age"":""23""},""BILLY"":{""id"":8,""age"":""29""}}"
Dim dict = JsonConvert.DeserializeObject(Of Dictionary(Of String, PersonData))(json)
For Each kvp In dict
Console.WriteLine("name: " & kvp.Key)
Console.WriteLine("id: " & kvp.Value.id)
Console.WriteLine("age: " & kvp.Value.age)
Console.WriteLine()
Next
小提琴:https://dotnetfiddle.net/HMv7Om
有意义?