1

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.

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

The json:

{"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"}}}

1
  • First, change the properties to Public otherwise you wont be able to access the data. Then the property names need to match the json: _id wont match id - get rid of the underscores. Also, the data should be a dictionary so you dont have to define a class for Fizz that is identical to "ziggy" Commented Jan 15, 2017 at 17:00

1 Answer 1

4

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.

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

usage:

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)

(Partial) Output:

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

Sign up to request clarification or add additional context in comments.

10 Comments

What tool are you referring to with Edit -> Paste Special -> Paste Json as Classes ?
Visual Studio - I think as of VS2010 @MrGadget I'll edit to make that clearer, it doesnt say what will create them
I found it...there're special steps for that (thanks, Google).
Not very special IMO - copy to clipboard, click, click
One does need a class file created and open and focused. If not, or you're focused on Solution Explorer, that doesn't work (I actually get Paste as XML classes with a clipboard full of JSON that does nothing...<shrug>).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.