title: JSON Encode and Decode with objects in Visual Basic 2010
body: <p>I have the following code:</p>
  
  <pre><code>Imports Newtonsoft.Json
  Imports Newtonsoft.Json.Linq
  Module Module1
  
  Structure JSONList
      Dim Name, Email As String
      Dim Age As Integer
  End Structure
  
  Sub Main()
      Dim Data(1) As JSONList
  
      Data(0).Name = "Josh"
      Data(0).Age = 17
      Data(0).Email = "me@mail.co.uk"
      Data(1).Name = "Greg"
      Data(1).Age = 17
      Data(1).Email = "greg@hotmail.co.uk"
  
      Dim JSONEncode As String
      JSONEncode = JsonConvert.SerializeObject(Data)
      Console.WriteLine(JSONEncode)
      Console.WriteLine()
      Console.WriteLine()
  
      Dim JSONDecode() As JSONList = JsonConvert.DeserializeObject(JSONEncode)
      Console.WriteLine(JSONDecode(0).Name)
  
      Console.ReadKey()
  
  
  End Sub
  
  End Module
  </code></pre>
  
  <p>The first encoding part of the script is used to store the encoded string to a database, the output is:</p>
  
  <pre><code>[{"Name":"Josh","Email":"me@mail.co.uk","Age":17},{"Name":"Greg","Email":"greg@hotmail.co.uk","Age":17}]
  </code></pre>
  
  <p>Now when I try to decode this JSON string, I get an error <code>Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'JSONList[]'.</code></p>
  
  <p>I need the data to be encoded in the JSON format so that I can use it in my website that uses PHP to decode it. I am using Visual Basic 2010 along with JSON.NET.</p>