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
JsonSerializer.Serializeturns properties into a JSON object.Deserialize(Of Reading)rebuilds the typed instance.- Doubled quotes escape inside a VB string literal.
Keywords and builtins used here
ClassDimDoubleEndImportsNewOfPropertyPublicReadingSiteStringValueWith
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.
Step 3 of 3 in Files & errors, step 27 of 29 in Language basics.