SteamAPI JSON.net错误在VB .NET [英] SteamAPI JSON.net Error in VB .net
问题描述
我真的不明白我做错了,当我尝试反序列化JSON的东西,我得到的。
I dont really understand what I do wrong when I try to Deserialize the JSON stuff I get.
您可以从 http://api.steampowered.com/响应ISteamApps / GetAppList / V2?格式= JSON
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim SteamGameInfoObject As Object = JsonConvert.DeserializeObject(Of Object)(New System.Net.WebClient().DownloadString("http://api.steampowered.com/ISteamApps/GetAppList/v2?format=json"))
End Sub
Public Class SteamRootObject
Public Class SteamAPIJSONResult
Public Property applist As List(Of applistlist)
End Class
Public Class applistlist
Public Property apps As List(Of appslist)
End Class
Public Class appslist
Public Property appid As Integer()
Public Property name As String()
End Class
End Class
End Class
我不知道如何为了解决这个错误要么改变的JSON到一个JSON数组
I dont know how to "To fix this error either change the JSON to a JSON array"
推荐答案
您在这里有几个问题:
-
您定义
的appid
和名称
分别为整数,字符串数组。在JSON这些原始值不是数组,所以VB.NET属性本身应该被宣布为简单的整数和字符串。
You define
appid
andname
as arrays of integers and strings, respectively. In the JSON these are primitive values not arrays, so the VB.NET properties themselves should be declared to be simple integers and strings.
您定义 APPLIST
为列表(中applistlist)
。尽管名字,在JSON的属性值是一个单一的对象不是一个数组,因此该属性应该是指一个对象也。
You define applist
as a List(Of applistlist)
. Despite the name, the property value in the JSON is a single object not an array, so the property should refer to a single object also.
在 Form1_Load的
你需要反序列化到正确的类型, SteamRootObject
。如果你这样做 JsonConvert.DeserializeObject(对象)(jsonString)
你是不是告诉Json.NET类型到反序列化!
In Form1_Load
you need to deserialize to the correct type, SteamRootObject
. If you do JsonConvert.DeserializeObject(Of Object)(jsonString)
you are not telling Json.NET the type to which to deserialize!
文体没有必要嵌套的根类内的内部类,它增加了复杂性。
Stylistically there is no need to nest the inner classes inside the root class, it adds to complexity.
有大量的在线工具从JSON自动生成.NET类,可以使用快速找到<一个href=\"https://encrypted.google.com/search?q=generate+vb.net+classes+from+json&ie=utf-8&oe=utf-8&aq=t\"相对=nofollow>谷歌,比如 http://json2csharp.com/ (对于C#)或 http://jsonutils.com/ 或的 http://www.httputility.net/json-to-csharp-vb-typescript-class.aspx 。今后,使用其中的一个来检查你的工作。
There are plenty of on-line tools to auto-generate .Net classes from JSON that can be found quickly using google, for instance http://json2csharp.com/ (for c#) or http://jsonutils.com/ or http://www.httputility.net/json-to-csharp-vb-typescript-class.aspx. In the future, use one of these to check your work.
因此,你的固定类应该是这样的:
Thus your fixed classes should look like:
Public Class App
Public Property appid As Integer
Public Property name As String
End Class
Public Class Applist
Public Property apps As New List(of App)
End Class
Public Class SteamRootObject
Public Property applist As Applist
End Class
和,反序列化:
Dim SteamGameInfoObject = JsonConvert.DeserializeObject(of SteamRootObject)(jsonString)
小提琴。
这篇关于SteamAPI JSON.net错误在VB .NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!