我正在使用 VB.net .Net Framework 4.7.2 中的 VS2019 并使用 Newtonsoft JSON 工具。
我有這個 JSON 示例訊息:
{
"status": "ok",
"items": [
{
"node": {
"id": 5,
"name": "HUB_SLS",
"type": "COMPANY",
"availableForBooking": true,
"device": false,
"shortName": "HSLS"
}
},
{
"node": {
"id": 6,
"name": "HQ1",
"type": "SITE",
"availableForBooking": true,
"device": false,
"shortName": "HQ1"
}
},
{
"node": {
"id": 7,
"name": "Short Term 1",
"type": "PARKING",
"availableForBooking": true,
"device": false,
"shortName": "ST1"
}
},
{
"node": {
"id": 7,
"name": "Entry 1- Sim",
"type": "entry_station",
"device": true,
"virtualParkingId": 7,
"shortName": "LESIM"
}
},
{
"node": {
"id": 8,
"name": "LX Sim 2",
"type": "exit_station",
"device": true,
"virtualParkingId": 7,
"shortName": "LXS2"
}
},
{
"node": {
"id": 9,
"name": "SIM - APS",
"type": "payment_station",
"device": true,
"virtualParkingId": 6,
"shortName": "SIMAP"
}
}
]
}
這是我定義的類
Public Class SitesDevices
Public Property status As String
Public Property items() As List(Of Node)
End Class
Public Class Node
Public Property id As Integer
Public Property name As String
Public Property type As String
Public Property availableForBooking As Boolean
Public Property virtualParkingId As Integer
Public Property device As Boolean
Public Property shortName As String
End Class
我來自網路呼叫的回應字串看起來不錯,并且包含預期值(3 個陣列元素)
Dim Response As String = client.DownloadString(myUri)
Dim LocationInfo As JMSClass.SitesDevices
LocationInfo = JsonConvert.DeserializeObject(Of JMSClass.SitesDevices)(Response)
"{""status"":""ok"",""items"":[{""node"":{""id"":5,""name"":""HUB_SLS"",""type"":""COMPANY"",""availableForBooking"":true,""device"":false,""shortName"":""HSLS""}},{""node"":{""id"":6,""name"":""HQ1"",""type"":""SITE"",""availableForBooking"":true,""device"":false,""shortName"":""HQ1""}},{""node"":{""id"":7,""name"":""Short Term 1"",""type"":""PARKING"",""availableForBooking"":true,""device"":false,""shortName"":""ST1""}}]}"
當我反序列化字串時,它確實回傳 3 個陣列項,但陣列項中的所有值都是“無”或真或零。
見下圖。

我努力了
Public Property items As Node()
and
Public Property items() As List(Of Node)
and
Public Property items() As NEW List(Of Node)
所有結果都相同。我對處理 JSON 并不陌生,但是,我在這一點上感到困惑,并認為這是非常微妙的事情。
uj5u.com熱心網友回復:
你必須以這種方式定義一個節點類
Public Class SitesDevices
Public Property status As String
Public Property items() As List(Of Node)
End Class
Public Class Node
Public Property node As NodeItem
End Class
Public Class NodeItem
Public Property id As Integer
Public Property name As String
Public Property type As String
Public Property availableForBooking As Boolean
Public Property virtualParkingId As Integer
Public Property device As Boolean
Public Property shortName As String
End Class
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/525272.html
