我目前正在开发一个vb.net win表单,它使用一个URL返回我需要放置到datatable中的json文件。我不知道我的公共类层次结构是如何做到的,而不生成数千行。现在,我正在尝试将所有信息放到文本框中,但最终会将它们放到datatable中,放置到datagridview中。这里是json文件和url的一个小部分,用于加载json文本字符串。
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代码,我总是手动地将信息写入一个datatable中,其中包含一个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
谢谢,
发布于 2019-09-23 20:37:39
你没有解释你是如何想要数据的,所以我把它放在一个文本框里。
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
https://stackoverflow.com/questions/58068313
复制相似问题