简体   繁体   中英

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

I am currently developing a vb.net win form that is using a URL to return a json file that I need to place into a datatable. I am not sure how do do my public class hierarchy without making thousands of lines. Right now I am trying to drop all the info into a textbox but will eventually put them into a datatable to place onto a datagridview. Here is a small porition of the json file and the url to load the json text string.

URL: https://api.worldoftanks.com/wot/encyclopedia/vehicles/?application_id=c2b5cb8d6c77098c8a9a481e68476b28&fields=name%2C+tank_id%2C+tier

Small sample of json file:

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

The problem here is under the "data" portion I am not used to having so many sub fields, in this case you see the following 1, 33, 49,.. etc. The only way I know how to create a public class for this would be to make an individual one for every single number and there are way too many for this. Can someone point me in the right direction for listing the 3 attributes: tier, name, and tank_id into a datatable. Also, for my other json code I have always manually written the information into a datatable with a for loop which has got to be the worst round about way of doing it so any help there would also be greatly appreciated.

Here is my vb.net code:

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

--------------------


I am currently trying this but because the id numbers are not in order and there are gaps in the id numbers, for example they may read 1 then 4 then 12, I get an error.

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

Thanks,

You didn't explain how you want the data so I just put it in a textbox.

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

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM