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

时间:2015-12-12 19:55:21

标签: json vb.net json.net deserialization

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

<div>

该类用于反序列化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
    }
  ]
}
]
}

我尝试使用代码反序列化:

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

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

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

1 个答案:

答案 0 :(得分:1)

您缺少课程中的整个课程。例如"38308613"是一个数组,其中包含以下[ ... ]之间的4个团队(??)。对于&#34; 65100402&#34;同样如此。根对象,然后包含这两件事,但你的尝试将它们定义为一个大数组。 (内容方面看起来它似乎与那两个错误地包含其他团队的团队混杂在一起,但是因为它是如何构建的,这就是它反序列化的方式)。

由于根对象(&#34;示例&#34;)实际上只是第一个{和最后},因此通常更容易摆脱它们。所以,要反序列化:

Dim jstr = ...from where ever

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

结果将是一个字典&#34; 38308613&#34;和&#34; 65100402&#34;作为钥匙。第一支有4支球队(?),最后一支持有3支球队。&#34; Cassiopeia的雇佣兵&#34;将是:

result("38308613")(0).name

来自&#34; 38308613&#34;的第一个(第0个)玩家ID的名称采集。迭代结果:

' 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&#39; Gath的Shadowdancers
  Elise的欺骗者
  Vayne的狙击手
  等