Results 1 to 2 of 2

Thread: reading json from file vb.net windows form app

  1. #1

    Thread Starter
    Registered User
    Join Date
    Sep 2024
    Posts
    1

    reading json from file vb.net windows form app

    I have made a windows form app to record scores for my friends when each person has a race on my race simulator.As it is now i manually put in the scores for finishing place. What i would like to do is have the app do it automatically by reading a json file that is created by the sim at the end of the race.I can read the whole file into a textbox text field and have been trying to work out the code to read data from the string using newtonsoft but the more example i look at the more confused I seem to get. The json file is below the part i after is the race result which is i may be wrong is an array within an array? i have special pasted the json as json properties





    {
    "track": "townsville_reworked",
    "number_of_sessions": 1,
    "players": [
    {
    "name": "Player",
    "car": "supercars_holden_zb",
    "skin": "daynight2020_huggins"
    },
    {
    "name": "David Reynolds",
    "car": "supercars_holden_zb",
    "skin": "2020_009_r01"
    },
    {
    "name": "Andre Heimgartner",
    "car": "supercars_holden_zb",
    "skin": "2022_008_r01"
    },
    {
    "name": "Scott Pye",
    "car": "supercars_holden_zb",
    "skin": "2022_020_r01"
    },
    {
    "name": "Shane VanGisbergen",
    "car": "supercars_holden_zb",
    "skin": "2020_097_r01"
    },
    {
    "name": "Chris Pither",
    "car": "supercars_holden_zb",
    "skin": "2022_022_r01"
    }],
    "sessions": [
    {
    "event": 0,
    "name": "Quick Race",
    "type": 3,
    "lapsCount": 5,
    "duration": 0,
    "laps": [],
    "lapstotal": [0, 0, 0, 0, 0, 0],
    "bestLaps": [],
    "raceResult": [1, 2, 3, 4, 5, 0]
    }],
    "extras": [
    {
    "name": "bestlap",
    "time": 0
    }]
    }

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,711

    Re: reading json from file vb.net windows form app

    I used your sample json to generate some vb classes, then used .net's build in json handling rather then newtonsoft - the following code should read your json into a set of objects.

    Code:
    Imports System.IO
    Imports System.Text.Json
    
    Module Program
        Sub Main(args As String())
           Dim json = File.ReadAllText("input.json")
    
           Dim scores = JsonSerializer.Deserialize(Of Root)(json)
           Console.WriteLine(scores.players(0).name)
       End Sub
        End Sub
    End Module
    
    Public Class Root
        Public Property track As String
        Public Property number_of_sessions As Integer
        Public Property players As List(Of Player)
        Public Property sessions As List(Of Session)
        Public Property extras As List(Of Extra)
    End Class
    
    Public Class Player
        Public Property name As String
        Public Property car As String
        Public Property skin As String
    End Class
    
    Public Class Session
        Public Property [event] As Integer
        Public Property name As String
        Public Property type As Integer
        Public Property lapsCount As Integer
        Public Property duration As Integer
        Public Property laps As List(Of Integer)
        Public Property lapstotal As List(Of Integer)
        Public Property bestLaps As List(Of Integer)
        Public Property raceResult As List(Of Integer)
    End Class
    
    Public Class Extra
        Public Property name As String
        Public Property time As Integer
    End Class
    after reading in the data you can access the information via the scores variable.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width