将Json对象反序列化为一个列表?

时间:2015-12-12 12:15:42

标签: json vb.net json.net deserialization

我正在尝试反序列化一个json文件,但无论我尝试什么,我都无法使它工作,所以我需要一些帮助。

所以这里是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
        }
      ]
    }
  ]
}

65100402/38308613属性对于每个api调用都是唯一的,基于webrequest上传递的ID(最多10个属性)。所以我的想法是创建一个通用类并将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 a As List(Of PlayerID) = JsonConvert.DeserializeObject(Of List(Of PlayerID))(json)
    MsgBox(a(1).name)

任何人都可以指出一种方法来反序列化它并循环遍历属性? 提前谢谢!

1 个答案:

答案 0 :(得分:0)

JSON不是列表;它由{}括起,而列表将由[]括起来。因此,您应该将其反序列化为对象类型。