我有以下代碼:JSON編碼和使用對象解碼在Visual Basic 2010
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
腳本的第一編碼部分,用於對編碼的字符串存儲到數據庫中,所述輸出爲:
[{"Name":"Josh","Email":"[email protected]","Age":17},{"Name":"Greg","Email":"[email protected]","Age":17}]
現在,當我嘗試這個JSON字符串解碼,我得到一個錯誤Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'JSONList[]'.
我需要的數據,以JSON格式進行編碼,這樣我可以在我的網站採用p使用惠普解碼它。我正在使用Visual Basic 2010和JSON.NET。