我目前正在开发一个vb.net win表单,该表单使用URL返回需要放入数据表中的json文件。我不确定在不进行成千上万行的情况下如何执行我的公共类层次结构。现在,我正在尝试将所有信息放入文本框,但最终会将它们放入数据表中,以放置到datagridview中。这是json文件和加载json文本字符串的url的一小部分。
网址:https://api.worldoftanks.com/wot/encyclopedia/vehicles/?application_id=c2b5cb8d6c77098c8a9a481e68476b28&fields=name%2C+tank_id%2C+tier
json文件的小样本:
{
"status": "ok",
"meta": {
"count": 632,
"page_total": 7,
"total": 632,
"limit": 100,
"page": null
},
"data": {
"1": {
"tier": 5,
"name": "T-34",
"tank_id": 1
},
"33": {
"tier": 5,
"name": "T14",
"tank_id": 33
},
"49": {
"tier": 8,
"name": "Type 59",
"tank_id": 49
},
"81": {
"tier": 1,
"name": "Vickers Medium Mk. I",
"tank_id": 81
},
"113": {
"tier": 1,
"name": "Kolohousenka",
"tank_id": 113
},
"129": {
"tier": 1,
"name": "Strv fm/21",
"tank_id": 129
},
"145": {
"tier": 6,
"name": "Pudel",
"tank_id": 145
},
"161": {
"tier": 1,
"name": "Fiat 3000",
"tank_id": 161
},
"257": {
"tier": 5,
"name": "SU-85",
"tank_id": 257
},
"273": {
"tier": 6,
"name": "Hummel",
"tank_id": 273
},
"289": {
"tier": 3,
"name": "M3 Stuart",
"tank_id": 289
},
"305": {
"tier": 7,
"name": "Type 62",
"tank_id": 305
},
"321": {
"tier": 3,
"name": "D2",
"tank_id": 321
}}
这里的问题是在“数据”部分下,我不习惯拥有太多子字段,在这种情况下,您会看到以下1、33、49等。我知道如何为该类创建公共类的唯一方法这将使每个数字都成为一个单独的数字,而这样做的方式太多了。有人可以指出将3个属性(层,名称和tank_id)列出到数据表中的正确方向。此外,对于我的其他json代码,我总是使用for循环将信息手动写入数据表中,这对于执行此方法来说是最糟糕的一轮,因此也将不胜感激。
这是我的vb.net代码:
Imports System.Net
Imports System.IO
Imports System.Web.Script.Serialization
Imports Newtonsoft.Json
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim uri1string As String = "https://api.worldoftanks.com/wot/encyclopedia/vehicles/?application_id=c2b5cb8d6c77098c8a9a481e68476b28&fields=short_name%2C+tank_id%2C+tier"
Dim uri1 As New Uri(uri1string)
Dim Request1 As HttpWebRequest = HttpWebRequest.Create(uri1)
Request.Method = "GET"
Dim Response1 As HttpWebResponse = Request1.GetResponse()
Dim read1 = New StreamReader(Response1.GetResponseStream())
Dim raw1 As String = read1.ReadToEnd()
Dim jss1 As New JavaScriptSerializer
Dim root1 As RootObject1 = jss1.Deserialize(Of RootObject1)(raw1)c
'------------------Trying to get this to work---------------
For Each vehicle As Vehicles In root1.data.First().Value.short_name
TextBox1.Text += vehicle.short_name + vbNewLine
Next
End Sub
End Class
Public Class RootObject1
Public Property status As String
Public Property meta As Meta1
Public Property data As Dictionary(Of String, Vehicles)
End Class
Public Class Meta1
Public Property count As Integer
End Class
Public Class Vehicles
Public Property tier As String
Public Property short_name As String
Public Property tank_id As String
End Class
'Public Class Vdesc
' Public Property tier As String
' Public Property short_name As String
' Public Property tank_id As String
'End Class
--------------------
我目前正在尝试此操作,但是由于ID编号不正确,并且ID编号中存在间隙,例如,它们可能读为1,然后是4,然后是12,则出现错误。
Dim tokenjson = JsonConvert.SerializeObject(uri1)
Dim jsonresult = JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(raw1)
Dim dtLookup As New DataTable
'dtLookup.Rows.Add()
dtLookup.Columns.Add()
dtLookup.Columns.Add()
dtLookup.Columns.Add()
For i As Integer = 0 To jsonresult.Count
dtLookup.Rows.Add()
dtLookup.Rows(i).Item(0) = jsonresult("data")("" & i + 1 & "")("tier")
dtLookup.Rows(i).Item(0) = jsonresult("data")("" & i + 1 & "")("name")
dtLookup.Rows(i).Item(0) = jsonresult("data")("" & i + 1 & "")("tank_id")
Next
谢谢,
最佳答案
您没有解释如何获取数据,因此只将其放在文本框中。
Imports Newtonsoft.Json.Linq
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim URL As String = "https://api.worldoftanks.com/wot/encyclopedia/vehicles/?application_id=c2b5cb8d6c77098c8a9a481e68476b28&fields=short_name%2C+tank_id%2C+tier"
Dim ParseJSON As JObject = JObject.Parse(New Net.WebClient().DownloadString(URL))
For Each tank As JProperty In ParseJSON("data")
Dim LineHolder As String = Nothing
For Each tankinfo As JProperty In tank.First
LineHolder &= String.Format("{0}: {1}, ", tankinfo.Name, tankinfo.Value)
Next
LineHolder = LineHolder.Remove(LineHolder.Length - 2)
TextBox2.AppendText(LineHolder & Environment.NewLine)
Next
End Sub
End Class
关于javascript - 从vb.net中的json文件上的数组返回多个元素,绑定(bind)到datatable,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58068313/