Reading json from file

TitanStor

New member
Joined
Oct 9, 2024
Messages
1
Programming Experience
Beginner
Hi. 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

JSON:
{
   "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
      }
   ]
}
 
Last edited by a moderator:
Which version of vb.net are you using? 4.8 Framework or newer?

EDIT:
I just used the latest (.Net 8) version and created a simple WinForm.
Click the button, select the json file, it reads a few items from it. I used Newtonsoft here as you don't really need to have an associated class to get the small bits you need.

vb.net using NewtonSoft:
Imports System.IO
Imports Newtonsoft.Json

Public Class Form1
    Private Sub btnReadJSON_Click(sender As Object, e As EventArgs) Handles btnReadJSON.Click

        Dim jsonFileDlg As New OpenFileDialog
        Dim dlgResult As DialogResult
        Dim jsonFilePath As String
        Dim raceResult As Linq.JObject
        Dim player As Linq.JObject

        jsonFileDlg.InitialDirectory = Application.StartupPath
        dlgResult = jsonFileDlg.ShowDialog()

        ' display an open file dialog box
        If dlgResult = DialogResult.OK Then
            ' a file was selected
            jsonFilePath = jsonFileDlg.FileName

            ' use Newton to read the JSON file
            raceResult = Linq.JObject.Parse(File.ReadAllText(jsonFilePath))

            lblTrackName.Text = raceResult("track")

            lbPlayers.Items.Clear()

            For Each player In raceResult("players")
                lbPlayers.Items.Add(player("name").ToString & " - " & player("car").ToString)
            Next

        Else
            MsgBox("No file selected.")
        End If


    End Sub

End Class

The project is attached,
 

Attachments

  • Screenshot 2024-10-15 052319.png
    Screenshot 2024-10-15 052319.png
    66.3 KB · Views: 1
  • SIMRacerResults.zip
    522.6 KB · Views: 0
Last edited:
Back
Top