Niel
Niel

Reputation: 141

How can I parse a JSON Array of objects to a list of VB.NET objects?

I installed JSON.Net through NuGet and now I want to parse a json array with objects to a list of objects in VB.NET. I have no idea where to begin.

My JSON array:

[
  {
    "servername": "US - New Jersey",
    "ovpnlocation": "servers/newjersey.ovpn"
  },
  {
    "servername": "The Netherlands",
    "ovpnlocation": "servers/nl.ovpn"
  },
  {
    "servername": "Belgium",
    "ovpnlocation": "servers/belgium.ovpn"
  }
]

I have a list of objects that I want to fill:

Dim ServerList As New List(Of ServerLocation)

And my ServerLocation class contains this:

Public Property ServerName As String
Public Property OVPNLocation As String

Upvotes: 1

Views: 3892

Answers (1)

b.pell
b.pell

Reputation: 4298

Using JSON.Net something like this should work, json being a string holding the JSON you listed above:

Dim ServerList As List(Of ServerLocation) = JsonConvert.DeserializeObject(Of List(Of ServerLocation))(json)

Upvotes: 4

Related Questions