反序列化 json 数组 return null
deserialize json array return null
嗯....试图将 json 反序列化为 "users" 的列表,但无法使其工作。
这里是 Json:
[{"Aatrox":{"version":"5.2.1","id":"Aatrox","key":"266","name":"Aatrox","title":"the Darkin Blade","blurb":"Aatrox is a legendary warrior, one of only five that remain of an ancient race known as the Darkin. He wields his massive blade with grace and poise, slicing through legions in a style that is hypnotic to behold. With each foe felled, Aatrox's ...","info":{"attack":8,"defense":4,"magic":3,"difficulty":4},"image":{"full":"Aatrox.png","sprite":"champion0.png","group":"champion","x":0,"y":0,"w":48,"h":48},"tags":["Fighter","Tank"],"partype":"BloodWell","stats":{"hp":537.8,"hpperlevel":85.0,"mp":105.6,"mpperlevel":45.0,"movespeed":345.0,"armor":24.384,"armorperlevel":3.8,"spellblock":32.1,"spellblockperlevel":1.25,"attackrange":150.0,"hpregen":6.59,"hpregenperlevel":0.5,"mpregen":0.0,"mpregenperlevel":0.0,"crit":0.0,"critperlevel":0.0,"attackdamage":60.376,"attackdamageperlevel":3.2,"attackspeedoffset":-0.04,"attackspeedperlevel":3.0}},"Ahri":{"version":"5.2.1","id":"Ahri","key":"103","name":"Ahri","title":"the Nine-Tailed Fox","blurb":"Unlike other foxes that roamed the woods of southern Ionia, Ahri had always felt a strange connection to the magical world around her; a connection that was somehow incomplete. Deep inside, she felt the skin she had been born into was an ill fit for ...","info":{"attack":3,"defense":4,"magic":8,"difficulty":5},"image":{"full":"Ahri.png","sprite":"champion0.png","group":"champion","x":48,"y":0,"w":48,"h":48},"tags":["Mage","Assassin"],"partype":"Mana","stats":{"hp":514.4,"hpperlevel":80.0,"mp":334.0,"mpperlevel":50.0,"movespeed":330.0,"armor":20.88,"armorperlevel":3.5,"spellblock":30.0,"spellblockperlevel":0.0,"attackrange":550.0,"hpregen":6.505,"hpregenperlevel":0.6,"mpregen":6.0,"mpregenperlevel":0.8,"crit":0.0,"critperlevel":0.0,"attackdamage":53.04,"attackdamageperlevel":3.0,"attackspeedoffset":-0.065,"attackspeedperlevel":2.0}}}]
我使用的代码:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim chlist As ChampionName() = JsonConvert.DeserializeObject(Of ChampionName())(jsonString)
TextBox1.Text += "Count: " & chlist.Count.ToString & vbNewLine
For Each s As ChampionName In chlist
TextBox1.Text += "Name: " & s.name & vbNewLine
Next
End Sub
我正在尝试传递数据的class:
Public Class Info
Public Property attack() As Integer
Get
Return m_attack
End Get
Set
m_attack = Value
End Set
End Property
Private m_attack As Integer
Public Property defense() As Integer
Get
Return m_defense
End Get
Set
m_defense = Value
End Set
End Property
Private m_defense As Integer
Public Property magic() As Integer
Get
Return m_magic
End Get
Set
m_magic = Value
End Set
End Property
Private m_magic As Integer
Public Property difficulty() As Integer
Get
Return m_difficulty
End Get
Set
m_difficulty = Value
End Set
End Property
Private m_difficulty As Integer
End Class
Public Class Image
Public Property full() As String
Get
Return m_full
End Get
Set
m_full = Value
End Set
End Property
Private m_full As String
Public Property sprite() As String
Get
Return m_sprite
End Get
Set
m_sprite = Value
End Set
End Property
Private m_sprite As String
Public Property group() As String
Get
Return m_group
End Get
Set
m_group = Value
End Set
End Property
Private m_group As String
Public Property x() As Integer
Get
Return m_x
End Get
Set
m_x = Value
End Set
End Property
Private m_x As Integer
Public Property y() As Integer
Get
Return m_y
End Get
Set
m_y = Value
End Set
End Property
Private m_y As Integer
Public Property w() As Integer
Get
Return m_w
End Get
Set
m_w = Value
End Set
End Property
Private m_w As Integer
Public Property h() As Integer
Get
Return m_h
End Get
Set
m_h = Value
End Set
End Property
Private m_h As Integer
End Class
Public Class Stats
Public Property hp() As Double
Get
Return m_hp
End Get
Set
m_hp = Value
End Set
End Property
Private m_hp As Double
Public Property hpperlevel() As Double
Get
Return m_hpperlevel
End Get
Set
m_hpperlevel = Value
End Set
End Property
Private m_hpperlevel As Double
Public Property mp() As Double
Get
Return m_mp
End Get
Set
m_mp = Value
End Set
End Property
Private m_mp As Double
Public Property mpperlevel() As Double
Get
Return m_mpperlevel
End Get
Set
m_mpperlevel = Value
End Set
End Property
Private m_mpperlevel As Double
Public Property movespeed() As Double
Get
Return m_movespeed
End Get
Set
m_movespeed = Value
End Set
End Property
Private m_movespeed As Double
Public Property armor() As Double
Get
Return m_armor
End Get
Set
m_armor = Value
End Set
End Property
Private m_armor As Double
Public Property armorperlevel() As Double
Get
Return m_armorperlevel
End Get
Set
m_armorperlevel = Value
End Set
End Property
Private m_armorperlevel As Double
Public Property spellblock() As Double
Get
Return m_spellblock
End Get
Set
m_spellblock = Value
End Set
End Property
Private m_spellblock As Double
Public Property spellblockperlevel() As Double
Get
Return m_spellblockperlevel
End Get
Set
m_spellblockperlevel = Value
End Set
End Property
Private m_spellblockperlevel As Double
Public Property attackrange() As Double
Get
Return m_attackrange
End Get
Set
m_attackrange = Value
End Set
End Property
Private m_attackrange As Double
Public Property hpregen() As Double
Get
Return m_hpregen
End Get
Set
m_hpregen = Value
End Set
End Property
Private m_hpregen As Double
Public Property hpregenperlevel() As Double
Get
Return m_hpregenperlevel
End Get
Set
m_hpregenperlevel = Value
End Set
End Property
Private m_hpregenperlevel As Double
Public Property mpregen() As Double
Get
Return m_mpregen
End Get
Set
m_mpregen = Value
End Set
End Property
Private m_mpregen As Double
Public Property mpregenperlevel() As Double
Get
Return m_mpregenperlevel
End Get
Set
m_mpregenperlevel = Value
End Set
End Property
Private m_mpregenperlevel As Double
Public Property crit() As Double
Get
Return m_crit
End Get
Set
m_crit = Value
End Set
End Property
Private m_crit As Double
Public Property critperlevel() As Double
Get
Return m_critperlevel
End Get
Set
m_critperlevel = Value
End Set
End Property
Private m_critperlevel As Double
Public Property attackdamage() As Double
Get
Return m_attackdamage
End Get
Set
m_attackdamage = Value
End Set
End Property
Private m_attackdamage As Double
Public Property attackdamageperlevel() As Double
Get
Return m_attackdamageperlevel
End Get
Set
m_attackdamageperlevel = Value
End Set
End Property
Private m_attackdamageperlevel As Double
Public Property attackspeedoffset() As Double
Get
Return m_attackspeedoffset
End Get
Set
m_attackspeedoffset = Value
End Set
End Property
Private m_attackspeedoffset As Double
Public Property attackspeedperlevel() As Double
Get
Return m_attackspeedperlevel
End Get
Set
m_attackspeedperlevel = Value
End Set
End Property
Private m_attackspeedperlevel As Double
End Class
Public Class ChampionName
Public Property version() As String
Get
Return m_version
End Get
Set
m_version = Value
End Set
End Property
Private m_version As String
Public Property id() As String
Get
Return m_id
End Get
Set
m_id = Value
End Set
End Property
Private m_id As String
Public Property key() As String
Get
Return m_key
End Get
Set
m_key = Value
End Set
End Property
Private m_key As String
Public Property name() As String
Get
Return m_name
End Get
Set
m_name = Value
End Set
End Property
Private m_name As String
Public Property title() As String
Get
Return m_title
End Get
Set
m_title = Value
End Set
End Property
Private m_title As String
Public Property blurb() As String
Get
Return m_blurb
End Get
Set
m_blurb = Value
End Set
End Property
Private m_blurb As String
Public Property info() As Info
Get
Return m_info
End Get
Set
m_info = Value
End Set
End Property
Private m_info As Info
Public Property image() As Image
Get
Return m_image
End Get
Set
m_image = Value
End Set
End Property
Private m_image As Image
Public Property tags() As List(Of String)
Get
Return m_tags
End Get
Set
m_tags = Value
End Set
End Property
Private m_tags As List(Of String)
Public Property partype() As String
Get
Return m_partype
End Get
Set
m_partype = Value
End Set
End Property
Private m_partype As String
Public Property stats() As Stats
Get
Return m_stats
End Get
Set
m_stats = Value
End Set
End Property
Private m_stats As Stats
End Class
Public Class RootObject
Public Property ChampionName() As ChampionName
Get
Return m_ChampionName
End Get
Set
m_ChampionName = Value
End Set
End Property
Private m_ChampionName As ChampionName
End Class
我没有收到任何错误,但也不是我想要的结果。
这是输出:image
如有任何帮助,我们将不胜感激!
从 VS2010 开始,您的 classes 实际上只需要自动属性;这使它们更易于编码和阅读:
Public Class Info
Public Property attack As Integer
Public Property defense As Integer
Public Property magic As Integer
Public Property difficulty As Integer
End Class
... etc
最后一个将是根对象
Public Class Players
Public Property Aatrox As Champion
Public Property Ahri As Champion
End Class
这些将用于反序列化数组或 List(of Player)
:
Dim jstr = File.ReadAllText("C:\Temp\aatrox.json")
Dim obj = JsonConvert.DeserializeObject(Of List(Of Players))(jstr)
如果你想要一个数组:
Dim obj = JsonConvert.DeserializeObject(Of Players())(jstr)
无论哪种方式,您最终都会得到一个 array/list,其中包含 1 个玩家项目,其中有 Ahri
和 Aatrox
属性 个对象。
json 有点傻,因为外部 [
将结果强制为一个数组。 "Aatrox": {...},"Ahri": {...}
的符号应该允许你反序列化为一个可以迭代的字典,但是 [...]
意味着我们必须把它放在一个数组中:
Dim temp = JsonConvert.DeserializeObject(Of Dictionary(Of String, Player)())(jstr)
' extract the player dictionary:
Dim players As Dictionary(Of String, Player) = temp(0)
For Each kvp As KeyValuePair(Of String, Player) In players
Console.WriteLine("Name: {0} Title: {1}",
kvp.Key, kvp.Value.title)
Next
每个的键将是名称,值将是 Player
class 对象:
Dim hp = players("Ahri").stats.hp
嗯....试图将 json 反序列化为 "users" 的列表,但无法使其工作。 这里是 Json:
[{"Aatrox":{"version":"5.2.1","id":"Aatrox","key":"266","name":"Aatrox","title":"the Darkin Blade","blurb":"Aatrox is a legendary warrior, one of only five that remain of an ancient race known as the Darkin. He wields his massive blade with grace and poise, slicing through legions in a style that is hypnotic to behold. With each foe felled, Aatrox's ...","info":{"attack":8,"defense":4,"magic":3,"difficulty":4},"image":{"full":"Aatrox.png","sprite":"champion0.png","group":"champion","x":0,"y":0,"w":48,"h":48},"tags":["Fighter","Tank"],"partype":"BloodWell","stats":{"hp":537.8,"hpperlevel":85.0,"mp":105.6,"mpperlevel":45.0,"movespeed":345.0,"armor":24.384,"armorperlevel":3.8,"spellblock":32.1,"spellblockperlevel":1.25,"attackrange":150.0,"hpregen":6.59,"hpregenperlevel":0.5,"mpregen":0.0,"mpregenperlevel":0.0,"crit":0.0,"critperlevel":0.0,"attackdamage":60.376,"attackdamageperlevel":3.2,"attackspeedoffset":-0.04,"attackspeedperlevel":3.0}},"Ahri":{"version":"5.2.1","id":"Ahri","key":"103","name":"Ahri","title":"the Nine-Tailed Fox","blurb":"Unlike other foxes that roamed the woods of southern Ionia, Ahri had always felt a strange connection to the magical world around her; a connection that was somehow incomplete. Deep inside, she felt the skin she had been born into was an ill fit for ...","info":{"attack":3,"defense":4,"magic":8,"difficulty":5},"image":{"full":"Ahri.png","sprite":"champion0.png","group":"champion","x":48,"y":0,"w":48,"h":48},"tags":["Mage","Assassin"],"partype":"Mana","stats":{"hp":514.4,"hpperlevel":80.0,"mp":334.0,"mpperlevel":50.0,"movespeed":330.0,"armor":20.88,"armorperlevel":3.5,"spellblock":30.0,"spellblockperlevel":0.0,"attackrange":550.0,"hpregen":6.505,"hpregenperlevel":0.6,"mpregen":6.0,"mpregenperlevel":0.8,"crit":0.0,"critperlevel":0.0,"attackdamage":53.04,"attackdamageperlevel":3.0,"attackspeedoffset":-0.065,"attackspeedperlevel":2.0}}}]
我使用的代码:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim chlist As ChampionName() = JsonConvert.DeserializeObject(Of ChampionName())(jsonString)
TextBox1.Text += "Count: " & chlist.Count.ToString & vbNewLine
For Each s As ChampionName In chlist
TextBox1.Text += "Name: " & s.name & vbNewLine
Next
End Sub
我正在尝试传递数据的class:
Public Class Info
Public Property attack() As Integer
Get
Return m_attack
End Get
Set
m_attack = Value
End Set
End Property
Private m_attack As Integer
Public Property defense() As Integer
Get
Return m_defense
End Get
Set
m_defense = Value
End Set
End Property
Private m_defense As Integer
Public Property magic() As Integer
Get
Return m_magic
End Get
Set
m_magic = Value
End Set
End Property
Private m_magic As Integer
Public Property difficulty() As Integer
Get
Return m_difficulty
End Get
Set
m_difficulty = Value
End Set
End Property
Private m_difficulty As Integer
End Class
Public Class Image
Public Property full() As String
Get
Return m_full
End Get
Set
m_full = Value
End Set
End Property
Private m_full As String
Public Property sprite() As String
Get
Return m_sprite
End Get
Set
m_sprite = Value
End Set
End Property
Private m_sprite As String
Public Property group() As String
Get
Return m_group
End Get
Set
m_group = Value
End Set
End Property
Private m_group As String
Public Property x() As Integer
Get
Return m_x
End Get
Set
m_x = Value
End Set
End Property
Private m_x As Integer
Public Property y() As Integer
Get
Return m_y
End Get
Set
m_y = Value
End Set
End Property
Private m_y As Integer
Public Property w() As Integer
Get
Return m_w
End Get
Set
m_w = Value
End Set
End Property
Private m_w As Integer
Public Property h() As Integer
Get
Return m_h
End Get
Set
m_h = Value
End Set
End Property
Private m_h As Integer
End Class
Public Class Stats
Public Property hp() As Double
Get
Return m_hp
End Get
Set
m_hp = Value
End Set
End Property
Private m_hp As Double
Public Property hpperlevel() As Double
Get
Return m_hpperlevel
End Get
Set
m_hpperlevel = Value
End Set
End Property
Private m_hpperlevel As Double
Public Property mp() As Double
Get
Return m_mp
End Get
Set
m_mp = Value
End Set
End Property
Private m_mp As Double
Public Property mpperlevel() As Double
Get
Return m_mpperlevel
End Get
Set
m_mpperlevel = Value
End Set
End Property
Private m_mpperlevel As Double
Public Property movespeed() As Double
Get
Return m_movespeed
End Get
Set
m_movespeed = Value
End Set
End Property
Private m_movespeed As Double
Public Property armor() As Double
Get
Return m_armor
End Get
Set
m_armor = Value
End Set
End Property
Private m_armor As Double
Public Property armorperlevel() As Double
Get
Return m_armorperlevel
End Get
Set
m_armorperlevel = Value
End Set
End Property
Private m_armorperlevel As Double
Public Property spellblock() As Double
Get
Return m_spellblock
End Get
Set
m_spellblock = Value
End Set
End Property
Private m_spellblock As Double
Public Property spellblockperlevel() As Double
Get
Return m_spellblockperlevel
End Get
Set
m_spellblockperlevel = Value
End Set
End Property
Private m_spellblockperlevel As Double
Public Property attackrange() As Double
Get
Return m_attackrange
End Get
Set
m_attackrange = Value
End Set
End Property
Private m_attackrange As Double
Public Property hpregen() As Double
Get
Return m_hpregen
End Get
Set
m_hpregen = Value
End Set
End Property
Private m_hpregen As Double
Public Property hpregenperlevel() As Double
Get
Return m_hpregenperlevel
End Get
Set
m_hpregenperlevel = Value
End Set
End Property
Private m_hpregenperlevel As Double
Public Property mpregen() As Double
Get
Return m_mpregen
End Get
Set
m_mpregen = Value
End Set
End Property
Private m_mpregen As Double
Public Property mpregenperlevel() As Double
Get
Return m_mpregenperlevel
End Get
Set
m_mpregenperlevel = Value
End Set
End Property
Private m_mpregenperlevel As Double
Public Property crit() As Double
Get
Return m_crit
End Get
Set
m_crit = Value
End Set
End Property
Private m_crit As Double
Public Property critperlevel() As Double
Get
Return m_critperlevel
End Get
Set
m_critperlevel = Value
End Set
End Property
Private m_critperlevel As Double
Public Property attackdamage() As Double
Get
Return m_attackdamage
End Get
Set
m_attackdamage = Value
End Set
End Property
Private m_attackdamage As Double
Public Property attackdamageperlevel() As Double
Get
Return m_attackdamageperlevel
End Get
Set
m_attackdamageperlevel = Value
End Set
End Property
Private m_attackdamageperlevel As Double
Public Property attackspeedoffset() As Double
Get
Return m_attackspeedoffset
End Get
Set
m_attackspeedoffset = Value
End Set
End Property
Private m_attackspeedoffset As Double
Public Property attackspeedperlevel() As Double
Get
Return m_attackspeedperlevel
End Get
Set
m_attackspeedperlevel = Value
End Set
End Property
Private m_attackspeedperlevel As Double
End Class
Public Class ChampionName
Public Property version() As String
Get
Return m_version
End Get
Set
m_version = Value
End Set
End Property
Private m_version As String
Public Property id() As String
Get
Return m_id
End Get
Set
m_id = Value
End Set
End Property
Private m_id As String
Public Property key() As String
Get
Return m_key
End Get
Set
m_key = Value
End Set
End Property
Private m_key As String
Public Property name() As String
Get
Return m_name
End Get
Set
m_name = Value
End Set
End Property
Private m_name As String
Public Property title() As String
Get
Return m_title
End Get
Set
m_title = Value
End Set
End Property
Private m_title As String
Public Property blurb() As String
Get
Return m_blurb
End Get
Set
m_blurb = Value
End Set
End Property
Private m_blurb As String
Public Property info() As Info
Get
Return m_info
End Get
Set
m_info = Value
End Set
End Property
Private m_info As Info
Public Property image() As Image
Get
Return m_image
End Get
Set
m_image = Value
End Set
End Property
Private m_image As Image
Public Property tags() As List(Of String)
Get
Return m_tags
End Get
Set
m_tags = Value
End Set
End Property
Private m_tags As List(Of String)
Public Property partype() As String
Get
Return m_partype
End Get
Set
m_partype = Value
End Set
End Property
Private m_partype As String
Public Property stats() As Stats
Get
Return m_stats
End Get
Set
m_stats = Value
End Set
End Property
Private m_stats As Stats
End Class
Public Class RootObject
Public Property ChampionName() As ChampionName
Get
Return m_ChampionName
End Get
Set
m_ChampionName = Value
End Set
End Property
Private m_ChampionName As ChampionName
End Class
我没有收到任何错误,但也不是我想要的结果。 这是输出:image
如有任何帮助,我们将不胜感激!
从 VS2010 开始,您的 classes 实际上只需要自动属性;这使它们更易于编码和阅读:
Public Class Info
Public Property attack As Integer
Public Property defense As Integer
Public Property magic As Integer
Public Property difficulty As Integer
End Class
... etc
最后一个将是根对象
Public Class Players
Public Property Aatrox As Champion
Public Property Ahri As Champion
End Class
这些将用于反序列化数组或 List(of Player)
:
Dim jstr = File.ReadAllText("C:\Temp\aatrox.json")
Dim obj = JsonConvert.DeserializeObject(Of List(Of Players))(jstr)
如果你想要一个数组:
Dim obj = JsonConvert.DeserializeObject(Of Players())(jstr)
无论哪种方式,您最终都会得到一个 array/list,其中包含 1 个玩家项目,其中有 Ahri
和 Aatrox
属性 个对象。
json 有点傻,因为外部 [
将结果强制为一个数组。 "Aatrox": {...},"Ahri": {...}
的符号应该允许你反序列化为一个可以迭代的字典,但是 [...]
意味着我们必须把它放在一个数组中:
Dim temp = JsonConvert.DeserializeObject(Of Dictionary(Of String, Player)())(jstr)
' extract the player dictionary:
Dim players As Dictionary(Of String, Player) = temp(0)
For Each kvp As KeyValuePair(Of String, Player) In players
Console.WriteLine("Name: {0} Title: {1}",
kvp.Key, kvp.Value.title)
Next
每个的键将是名称,值将是 Player
class 对象:
Dim hp = players("Ahri").stats.hp