0

I'm attempting to make a program for LoL that would allow users to view specific stats for a player. However, I'm having issues..

The error I'm getting is.

Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'WindowsApplication1.Form1+Rootobject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

This is the code that I have so far.

Public Class Rootobject
        Public Property games As Integer
        Public Property winPercent As Double
        Public Property order As List(Of Class1)
        Public Property role As String
    End Class

    Public Class Class1
        Public Property order() As String
    End Class

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim client As WebClient = New WebClient()
        client.Headers.Add("Content-Type", "application/json")
        Dim reply As String = client.DownloadString("skills/mostPopular?api_key=")
        Dim rootObject As Rootobject = JsonConvert.DeserializeObject(Of Rootobject)(reply)
        MsgBox(rootObject.games)
    End Sub 

Here is how the JSON looks like. (Id post a URL but it requires an API key)

[
  {
    "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"
  }
] 
1
  • When trying to match up .NET object structure with JSON, I quite often create a temporary method, create a new temp object (in your case an instance of Rootobject fill it in with dummy data (making sure that I've populated each serializable property) and then serialize it to JSON. Then I know what JSON is expected and I'm not in a dark. Commented Nov 25, 2016 at 17:59

2 Answers 2

3

Update the root object. The JSON in question resolves to the below class

Public Class Rootobject
    Public Property games As Integer
    Public Property winPercent As Double
    Public Property order As String()
    Public Property role As String
End Class

You are also trying to deserialize the array into a single object when the data is a collection.

Dim rootObjects As List(Of Rootobject) = JsonConvert.DeserializeObject(Of List(Of Rootobject))(reply)
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome this works! How would I iterate the data in order would it be a for loop?
A For or ForEach will work as order is enumerable.
3

Your Json string is array of RootObjects. Change expected object to the list

Dim objects As List(Of Rootobject) = JsonConvert.DeserializeObject(Of List(Of Rootobject))(reply)

Dim root As Rootobject = objects.First()
MessageBox.Show(root.games)

And second error noticed by @Nkosi - change Rootobject as he suggest

1 Comment

Thanks for all your help!

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.