JSON.Net无法反序列化当前JSON数组 [英] JSON.Net Cannot deserialize the current JSON array

查看:113
本文介绍了JSON.Net无法反序列化当前JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为LoL制作一个程序,该程序将允许用户查看玩家的特定统计信息.但是,我遇到了问题.

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..

我得到的错误是.

其他信息:无法反序列化当前JSON数组 (例如[1,2,3])转换为类型'WindowsApplication1.Form1 + Rootobject' 因为该类型需要一个JSON对象(例如{"name":"value"}) 正确反序列化.

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 

这是JSON的样子. (Id发布网址,但需要API密钥)

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

推荐答案

更新根对象.有问题的JSON解析为以下类

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)

这篇关于JSON.Net无法反序列化当前JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆