Return vb.net 中 json 文件数组中的多个元素,绑定到数据表

Return multiple elements from array on json file in vb.net, bind to datatable

我目前正在开发一个 vb.net win 表单,它使用 URL 到 return 我需要放入数据表中的 json 文件。我不确定我的 public class 层次结构如何才能不生成数千行。现在我正试图将所有信息放入文本框中,但最终会将它们放入数据表中以放置到数据网格视图中。这是 json 文件的一小部分和用于加载 json 文本字符串的 url。

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

这里的问题是在 "data" 部分下 我不习惯有这么多子字段,在这种情况下你会看到以下 1, 33, 49,.. 等。我知道的唯一方法如何为此创建一个 public class 将是为每个数字制作一个单独的数字,而这个数字太多了。有人能给我指明正确的方向,以便将 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