I have the following 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 = "[email protected]"
Data(1).Name = "Greg"
Data(1).Age = 17
Data(1).Email = "[email protected]"
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
The first encoding part of the script is used to store the encoded string to a database, the output is:
[{"Name":"Josh","Email":"[email protected]","Age":17},{"Name":"Greg","Email":"[email protected]","Age":17}]
Now when I try to decode this JSON string, I get an error Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'JSONList[]'.
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.