-1

I'm wanting to start diving into some API's that this site uses. http://api.champion.gg/

However, I'm having some trouble understanding on how to grab specific data. For example, one of the API url's returns this data.

    [
  {
    "games": 1650,
    "winPercent": 46.9,
    "order": [
      "Q",
      "W",
      "E",
      "Q",
      "Q",
      "R",
      "Q",
      "W",
      "Q",
      "W",
      "R",
      "W",
      "W",
      "E",
      "E",
      "R",
      "E",
      "E"
    ],
    "role": "Support"
  },
  {
    "games": 9769,
    "winPercent": 51.8,
    "order": [
      "Q",
      "W",
      "E",
      "Q",
      "Q",
      "R",
      "Q",
      "W",
      "Q",
      "W",
      "R",
      "W",
      "W",
      "E",
      "E",
      "R",
      "E",
      "E"
    ],
    "role": "Middle"
  }
]

And id like to grab

  • games
  • winPercent
  • order (I understand this this is an array?)

Could I generate a JSON Class using a JSON Class Generator? Would that work? And if so how would I use it.

Edit: I generated the class..

So my class looks like

`Public Class Class1
    Public Property games As Integer
    Public Property winPercent As Single
    Public Property order() As String
    Public Property role As String
End Class`

Would I be able to do?

`        Dim obj = JsonConvert.DeserializeObject(Of Class1)(RichTextBox1.Text)
        MsgBox(obj.games)`
DoomBots
  • 13
  • 3
  • 3
    Visual Studio will create the classes for you: copy the json to the clipboard, then **Edit Menu -> Paste Special -> Paste Json as classes** or use an online robot if you have an older version: http://jsonutils.com/ – Ňɏssa Pøngjǣrdenlarp Nov 22 '16 at 19:06
  • Thank you! How would I use these classes? – DoomBots Nov 22 '16 at 22:02
  • Are you asking how to download and deserialize JSON in c#? There are already lots of questions related to that, for instance [deserializing JSON to .net object using NewtonSoft (or linq to json maybe?)](https://stackoverflow.com/questions/4749639/deserializing-json-to-net-object-using-newtonsoft-or-linq-to-json-maybe). – dbc Nov 22 '16 at 22:39
  • I've updated the OP with further questions. – DoomBots Nov 23 '16 at 00:26
  • When you post on SO, you are not opening a support ticket where you keep adding new issues to the post until we fix all your issues. Ask one question per post. There are gobs of questions here on deserializing. `Would I be able to do...` is a **bad question** because you can press a button and find out for yourself. Please read [ASk] and take the [tour]. – Ňɏssa Pøngjǣrdenlarp Nov 23 '16 at 02:13
  • It's not a new issue. It was a question, I suppose I should have listened to the other forums when people said that SO is filled with people who act like entitled assholes. There's a way you can tell somebody that they did something wrong, and the way you did it was completely unnecessary and rude. Especially to someone who's new, and never really posted on these type of sites before. I updated my question with the information you provided. – DoomBots Nov 23 '16 at 03:34

1 Answers1

1

Your class is correct and you can use it for deserializing your JSON as follows:

var result = JsonConvert.DeserializeObject<List<Class1>>(json);

Then, using a loop (e.g. foreach) you can access the elements of result.

foreach (var item in result)
{
    Console.WriteLine(item.games);
}

This code is in C# but you get the idea.

Mahdi
  • 3,199
  • 2
  • 25
  • 35