JSON反序列化为VB.Net对象

JSON反序列化为VB.Net对象

问题描述:

我还阅读了其他答案,但我似乎仍然迷路.我正在尝试将JSON引入对象.我通过API调用JSON字符串,我想将其转换为RequestManager之后的对象.我是否缺少方法或其他东西?另外,returnChampions2函数是错误的.我在方法之后发布了JSON字符串的一部分,它真的很长.

I've read other answers, but I still seem lost. I am trying to bring JSON into my objects. I call a JSON string through an API, and I want to convert it into the objects after the RequestManager. Am I missing methods or something? Also, the returnChampions2 function is wrong. I posted part of the JSON string after the methods, it's really long.

公共类RequestManager

Public Class RequestManager

Public Function returnChampions2(ByVal strRegion As String) As LeagueChampionMaster
    Dim strRequest As String = "https://global.api.pvp.net/api/lol/static-data/" + strRegion + "/v1.2/champion?api_key=" + _APIKey
    Return JsonConvert.DeserializeObject(Of LeagueChampionMaster)(returnJSONRequest(strRequest))
End Function

Public Class LeagueChampionMaster
    Private _Type As String
    Private _Version As String
    Private _Data As LeagueChampion()
End Class

Public Class LeagueChampion
    Private _ID As Integer
    Private _Key As String
    Private _Name As String
    Private _Title As String
End Class

json:

{"type":"champion","version":"6.24.1","data":{"Jax":{"id":24,"key":"Jax","name": "Jax","title":大师 at Arms}," Sona:{" id:37," key:" Sona," name:" Sona," title:" Maven of 这 Strings}," Tristana:{" id:18," key:" Tristana," name:" Tristana," title:" the 约德尔 Gunner}," Varus:{" id:110," key:" Varus," name:" Varus," title:" the 的箭 Retribution}," Fiora:{" id:114," key:" Fiora," name:" Fiora," title:" the 盛大 Duelist}," Singed:{" id:27," key:" Singed," name:" Singed," title:" the 疯狂化学家}," TahmKench:{" id:223," key:" TahmKench," name:" Tahm Kench",标题":"The River" King}," Leblanc:{" id:7," key:" Leblanc," name:" LeBlanc," title:" the Deceiver}," Thresh:{" id:412," key:" Thresh," name:" Thresh," title:" the 链 守望者},"业力:{" id:43,"键:"业力,"名称:"业力,"标题:" 开明 一个}," Jhin:{" id:202," key:" Jhin," name:" Jhin," title:" the Virtuoso}," Rumble:{" id:68," key:" Rumble," name:" Rumble," title:" the 机械化 威胁}," Udyr:{" id:77," key:" Udyr," name:" Udyr," title:" the Spirit Walker}," LeeSin:{" id:64," key:" LeeSin," name:" Lee 罪,"标题:"盲人 和尚}," Yorick:{" id:83," key:" Yorick," name:" Yorick," title:" Shepherd 的 灵魂}," Kassadin:{" id:38," key:" Kassadin," name:" Kassadin," title:" the 空白 Walker}," Sivir:{" id:15," key:" Sivir," name:" Sivir," title:" the 战斗 情妇}," MissFortune:{" id:21," key:" MissFortune," name:" Miss 《财富》,《标题》:《赏金》 Hunter}," Draven:{" id:119," key:" Draven," name:" Draven," title:" the 辉煌 e子手}},"Yasuo":{"id":157,"key":"Yasuo","name":"Yasuo","title":"the Unforgiven}," Kayle:{" id:10," key:" Kayle," name:" Kayle," title:" The Judicator}," Shaco:{" id:35," key:" Shaco," name:" Shaco," title:" the 恶魔 弄臣(Jester)}," Renekton:{" id:58," key:" Renekton," name:" Renekton," title:" the 的屠夫 Sands}," Hecarim:{" id:120," key:" Hecarim," name:" Hecarim," title:" the 的影子 War}," Fizz:{" id:105," key:" Fizz," name:" Fizz," title:" The Tidal 骗子}}}

{"type":"champion","version":"6.24.1","data":{"Jax":{"id":24,"key":"Jax","name":"Jax","title":"Grandmaster at Arms"},"Sona":{"id":37,"key":"Sona","name":"Sona","title":"Maven of the Strings"},"Tristana":{"id":18,"key":"Tristana","name":"Tristana","title":"the Yordle Gunner"},"Varus":{"id":110,"key":"Varus","name":"Varus","title":"the Arrow of Retribution"},"Fiora":{"id":114,"key":"Fiora","name":"Fiora","title":"the Grand Duelist"},"Singed":{"id":27,"key":"Singed","name":"Singed","title":"the Mad Chemist"},"TahmKench":{"id":223,"key":"TahmKench","name":"Tahm Kench","title":"the River King"},"Leblanc":{"id":7,"key":"Leblanc","name":"LeBlanc","title":"the Deceiver"},"Thresh":{"id":412,"key":"Thresh","name":"Thresh","title":"the Chain Warden"},"Karma":{"id":43,"key":"Karma","name":"Karma","title":"the Enlightened One"},"Jhin":{"id":202,"key":"Jhin","name":"Jhin","title":"the Virtuoso"},"Rumble":{"id":68,"key":"Rumble","name":"Rumble","title":"the Mechanized Menace"},"Udyr":{"id":77,"key":"Udyr","name":"Udyr","title":"the Spirit Walker"},"LeeSin":{"id":64,"key":"LeeSin","name":"Lee Sin","title":"the Blind Monk"},"Yorick":{"id":83,"key":"Yorick","name":"Yorick","title":"Shepherd of Souls"},"Kassadin":{"id":38,"key":"Kassadin","name":"Kassadin","title":"the Void Walker"},"Sivir":{"id":15,"key":"Sivir","name":"Sivir","title":"the Battle Mistress"},"MissFortune":{"id":21,"key":"MissFortune","name":"Miss Fortune","title":"the Bounty Hunter"},"Draven":{"id":119,"key":"Draven","name":"Draven","title":"the Glorious Executioner"},"Yasuo":{"id":157,"key":"Yasuo","name":"Yasuo","title":"the Unforgiven"},"Kayle":{"id":10,"key":"Kayle","name":"Kayle","title":"The Judicator"},"Shaco":{"id":35,"key":"Shaco","name":"Shaco","title":"the Demon Jester"},"Renekton":{"id":58,"key":"Renekton","name":"Renekton","title":"the Butcher of the Sands"},"Hecarim":{"id":120,"key":"Hecarim","name":"Hecarim","title":"the Shadow of War"},"Fizz":{"id":105,"key":"Fizz","name":"Fizz","title":"the Tidal Trickster"}}}

您遇到了几个问题.您应该知道,如果将json复制到剪贴板,则编辑-> 选择性粘贴-> 将Json粘贴为类,Visual Studio会创建课程为您提供一个体面的起点.在 this 情况下,该工具有点密集,将为"Fizz","Shaco"等创建多个相同的类.您已经对其进行了标准化.

You have several issues. You should know that if you copy the json to the clipboard, Edit -> Paste Special -> Paste Json as Classes Visual Studio will create the classes for you to give you a decent starting point. In this case, the tool is a little dense and will create umpteen identical classes for "Fizz", "Shaco" etc. You have already normalized that.

但是,使用所有属性Private,您将无法访问数据.然后,属性名称是错误的. idkey的json键不会映射/反序列化到_id_key,因为它们不匹配.最后,数据收集应为Dictionary.每个播放器/冠军/物品的键将用作Dictionary键:

However, with all the properties Private you wont be able to access the data. Then, the property names are wrong. The json key of id or key will not map/deserialize to _id or _key because they do not match. Lastly, the data collection should be a Dictionary. The key for each player/champion/item will be used as the Dictionary key:

Public Class LeagueContainer
    Public Property type As String
    Public Property version As String
    Public Property data As Dictionary(Of String, DataItem)
End Class

Public Class DataItem
    Public Property id As Integer
    Public Property key As String
    Public Property name As String
    Public Property title As String
End Class

用法:

Dim jstr = ... from where ever ...
Dim myData = JsonConvert.DeserializeObject(Of LeagueContainer)(jstr)

' print the keys
For Each kvp In myData.data
    Console.WriteLine(kvp.Key)
Next

' what is Yorick's title?
Console.WriteLine("Yorick is '{0}'", myData.data("Yorick").title)

(部分)输出:

Jax
索娜
特里斯塔纳
内窥镜
菲奥拉
...
约里克(Yorick)是灵魂的牧羊人"

Jax
Sona
Tristana
Varus
Fiora
...
Yorick is 'Shepherd of Souls'