JSON-Kodierung und-Dekodierung mit Objekten in Visual Basic 2010

Ich habe den folgenden 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

Den ersten Codierung Teil des Skripts verwendet wird, zum speichern der kodierten string einer Datenbank, die Ausgabe ist:

[{"Name":"Josh","Email":"[email protected]","Age":17},{"Name":"Greg","Email":"[email protected]","Age":17}]

Wenn ich jetzt versuche zu entschlüsseln dieser JSON-string, bekomme ich eine Fehlermeldung Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'JSONList[]'.

Ich brauche die Daten werden codiert im JSON-format, so dass ich kann es in meiner website verwendet PHP zu entschlüsseln. Ich benutze Visual Basic 2010 zusammen mit JSON.NET.

InformationsquelleAutor Josh Luke Blease | 2013-09-23

Schreibe einen Kommentar