无需一个多个属性反序列化JSON

时间:2018-01-07 18:51:31

标签: json vb.net json.net

我有这个JSON:

  

{ “JOE”:{ “ID”:7, “年龄”: “23”}, “比利”:{ “ID”:8, “年龄”: “29”}}

我有this solution for a more simple JSON structure posted by RajN

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数据。 提前致谢

1 个答案:

答案 0 :(得分:1)

解决方案与@RajN的the other solution you linked几乎相同,除了使用Dictionary(Of String, String),您需要使用Dictionary(Of String, T),其中T是您定义的类保留idage

所以,定义一个类:

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

有意义吗?