typestar

JSON in Visual Basic

System.Text.Json round trips for a class and a plain array.

Imports System.Text.Json

Dim probe As New Reading With {.Site = "north", .Value = 7.25}
Dim payload = JsonSerializer.Serialize(probe)
Console.WriteLine(payload)

Dim back = JsonSerializer.Deserialize(Of Reading)(payload)
Console.WriteLine($"{back.Site} reads {back.Value}")

Dim tags = JsonSerializer.Deserialize(Of String())("[""wind"", ""rain""]")
Console.WriteLine(tags.Length)

Class Reading
    Public Property Site As String
    Public Property Value As Double
End Class

How it works

  1. JsonSerializer.Serialize turns properties into a JSON object.
  2. Deserialize(Of Reading) rebuilds the typed instance.
  3. Doubled quotes escape inside a VB string literal.

Keywords and builtins used here

The run, in numbers

Lines
16
Characters to type
469
Tokens
110
Three-star pace
80 tpm

At the three-star pace of 80 tokens a minute, this run takes about 82 seconds.

Type this snippet

Step 3 of 3 in Files & errors, step 27 of 29 in Language basics.

← Previous Next →

JSON in other languages