How to add a certain value to an Integer of a JSON in vb.net?

2

Hello, I have this code:

    Imports System.IO
    Imports Newtonsoft.Json.Linq
    Public Class Form1
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Dim channel As JObject = JObject.Parse(File.ReadAllText("C:\stats.json"))
            Dim points = channel.GetValue("points").ToString()

            Label1.Text = points
        End Sub

And I have a button, which, in the case, I want every time it is pressed, add +30 to points . How to do this? Thanks in advance.

    
asked by anonymous 04.09.2018 / 03:48

1 answer

0

Try doing the following:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Try
        Dim channel As JObject = JObject.Parse(File.ReadAllText("C:\stats.json"))

        With channel
            'Adicionar 30 pontos
            .Item("points") = Convert.ToInt32(.Item("points")) + 30
        End With

        'Serializar o objeto
        Dim result As String = JsonConvert.SerializeObject(channel)

        File.WriteAllText("C:\stats.json", result)
    Catch ex As Exception
        MsgBox("Ocorreu um erro: " + ex.Message)
    End Try
End Sub
    
04.09.2018 / 16:39