正在读取反序列化的json抛出null引用



虽然我每次尝试读取数据时都可以将json反序列化为对象(或者至少没有错误),但它会引发null引用异常。这是json:

{
 "38308613": [
{
  "name": "Cassiopeia's Mercenaries",
  "tier": "GOLD",
  "queue": "RANKED_SOLO_5x5",
  "entries": [
    {
      "playerOrTeamId": "38308613",
      "playerOrTeamName": "TrashTheBrain",
      "division": "III",
      "leaguePoints": 75,
      "wins": 328,
      "losses": 321,
      "isHotStreak": false,
      "isVeteran": true,
      "isFreshBlood": false,
      "isInactive": false
    }
  ]
},
{
  "name": "Cho'Gath's Shadowdancers",
  "tier": "GOLD",
  "queue": "RANKED_TEAM_3x3",
  "entries": [
    {
      "playerOrTeamId": "TEAM-8bf3a360-77ba-11e4-9fc4-c81f66db96d8",
      "playerOrTeamName": "Ebal 9 vas",
      "division": "IV",
      "leaguePoints": 0,
      "wins": 22,
      "losses": 24,
      "isHotStreak": false,
      "isVeteran": false,
      "isFreshBlood": false,
      "isInactive": false
    }
  ]
},
{
  "name": "Elise's Deceivers",
  "tier": "BRONZE",
  "queue": "RANKED_TEAM_5x5",
  "entries": [
    {
      "playerOrTeamId": "TEAM-8bf3a360-77ba-11e4-9fc4-c81f66db96d8",
      "playerOrTeamName": "Ebal 9 vas",
      "division": "III",
      "leaguePoints": 66,
      "wins": 5,
      "losses": 6,
      "isHotStreak": false,
      "isVeteran": false,
      "isFreshBlood": false,
      "isInactive": false
    }
  ]
},
{
  "name": "Vayne's Snipers",
  "tier": "SILVER",
  "queue": "RANKED_TEAM_3x3",
  "entries": [
    {
      "playerOrTeamId": "TEAM-7da3b7b0-7bfa-11e5-a148-c81f66dd30e5",
      "playerOrTeamName": "MathMechPower",
      "division": "V",
      "leaguePoints": 75,
      "wins": 7,
      "losses": 6,
      "isHotStreak": false,
      "isVeteran": false,
      "isFreshBlood": false,
      "isInactive": false
    }
  ]
  }
  ],
 "65100402": [
{
  "name": "Jarvan IV's Spellslingers",
  "tier": "GOLD",
  "queue": "RANKED_SOLO_5x5",
  "entries": [
    {
      "playerOrTeamId": "65100402",
      "playerOrTeamName": "Charlynnnn",
      "division": "I",
      "leaguePoints": 8,
      "wins": 447,
      "losses": 438,
      "isHotStreak": false,
      "isVeteran": true,
      "isFreshBlood": false,
      "isInactive": false
    }
  ]
},
{
  "name": "Darius's Warlocks",
  "tier": "SILVER",
  "queue": "RANKED_TEAM_3x3",
  "entries": [
    {
      "playerOrTeamId": "TEAM-f86481d0-6cf2-11e5-a39d-c81f66dd30e5",
      "playerOrTeamName": "YingYangWolves",
      "division": "II",
      "leaguePoints": 0,
      "wins": 4,
      "losses": 2,
      "isHotStreak": false,
      "isVeteran": false,
      "isFreshBlood": false,
      "isInactive": false
    }
  ]
},
{
  "name": "Morgana's Elementalists",
  "tier": "SILVER",
  "queue": "RANKED_TEAM_5x5",
  "entries": [
    {
      "playerOrTeamId": "TEAM-f86481d0-6cf2-11e5-a39d-c81f66dd30e5",
      "playerOrTeamName": "YingYangWolves",
      "division": "III",
      "leaguePoints": 0,
      "wins": 3,
      "losses": 3,
      "isHotStreak": false,
      "isVeteran": false,
      "isFreshBlood": false,
      "isInactive": false
    }
  ]
}
]
}

以及用于反序列化json:的类

Public Class Entry
    Public Property playerOrTeamId As String
    Public Property playerOrTeamName As String
    Public Property division As String
    Public Property leaguePoints As Integer
    Public Property wins As Integer
    Public Property losses As Integer
    Public Property isHotStreak As Boolean
    Public Property isVeteran As Boolean
    Public Property isFreshBlood As Boolean
    Public Property isInactive As Boolean
End Class
    Public Class PlayerID
    Public Property name As String
    Public Property tier As String
    Public Property queue As String
    Public Property entries As Entry()
End Class
Public Class Example
    Public Property PlayerID As PlayerID()
End Class

我尝试使用以下代码进行反序列化:

 Dim output As New PlayerID 
    output = JsonConvert.DeserializeObject(Of Example)(TextBox1.Text)
    For Each i In output.PlayerID 
        MsgBox(i.name)
    Next

此外,我试图将Example类转换为List(Of PlayerId),但仍然存在null引用。

那么,有人能指出反序列化和遍历属性的正确方法吗?无论我尝试什么,json都不是我的爱好。提前感谢!

您的类中缺少一整层。例如,"38308613"是一个数组,包含以下[ ... ]之间的4个团队(??)。"65100402"也是如此。根对象包含这两个东西,但您的根对象试图将它们定义为一个大数组。(从内容上看,它似乎与这两个团队混杂在一起,错误地包含了其他团队,但由于这就是它的结构,因此它将反序列化)。

由于根对象("Example")实际上只是第一个{和最后一个},因此通常更容易去掉它们。因此,要反序列化:

Dim jstr = ...from where ever
Dim result = JsonConvert.DeserializeObject(Of Dictionary(Of String, PlayerID()))(jstr)

结果将是一个以"38308613"one_answers"65100402"为关键字的字典。第一队有4支球队,最后一队有3支。"仙后座的雇佣军"应该是:

result("38308613")(0).name

"38308613"集合中第一个(第0个)PlayerID的名称。迭代结果:

' print all team  names
For Each kvp As KeyValuePair(Of String, PlayerID()) In result
    Console.WriteLine("This group: {0}", kvp.Key)
    For Each p As PlayerID In kvp.Value
        Console.WriteLine(p.name)
    Next
    Console.WriteLine()
Next

输出:

此组:38308613
仙后座的雇佣军
Cho'Gath的影子舞者
Elise的骗子
Vayne的狙击手
etc

相关内容

  • 没有找到相关文章

最新更新