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

Asked

Viewed 32 times

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 this case, I want every time it’s pressed, to add +30 on points. How to do this? Thanks in advance.

  • The only place where it has the value of points is in a Label? There’s no class where you store it?

  • No, there is a module. But I want to add 30 more points to the JSON by clicking the button.

  • Ha, in JSON! IE, you want to take the object that came from your JSON file, add 30 and then re-save the file but with the points updated? That’s it?

  • Yes! That’s right, that’s right!

1 answer

0


Try doing it this way:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.