我有一個問題,我不知道如何解決。JSON JsonConvert.DeserializeObject錯誤
我試圖解決錯誤:
Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'name.jsonPrjData' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly
我發現對堆棧溢出類似的網頁,並試圖什麼的答案中提出的,但我無法弄清楚是什麼問題。
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Public Class jsonPrjData
Public Property sapcode() As String
Public Property prjCode() As String
Public Property prjDescript() As String
End Class
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString("http://localhost/json/")
Dim obj = JsonConvert.DeserializeObject(Of jsonPrjData)(result)
End Sub
End Class
而且JSON看起來是這樣的:
[{"sapcode":"xxxx","prjCode":"xxxx","prjDescript":"xxxx"},{"sapcode":"xxxx","prjCode":"xxxx","prjDescript":"xxxx"}]
我認爲它是因爲您試圖將一個json對象數組反序列化爲一個jsonPrjData .NET對象。你需要desirialize到一個集合或列表。 – yardpenalty
我該怎麼做? – Starlays