Search code examples
arraysjsonvb.netjson.netdeserialization

Trouble deserializing a json array using newtonsoft - array returned but all elements = nothing


I am working with VS2019 in VB.net .Net Framework 4.7.2 and using Newtonsoft JSON tool.

I have this JSON sample message:

    {
    "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"
            }
        }
    ]
}

And this is my defined class

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

My response string from the web call looks good and contains the expected values (3 array elements)

  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""}}]}"

When I deserialize the string, it does return 3 array items, but all the values in the array items are "nothing" or true or zero.

See image below.

enter image description here

I have tried

Public Property items As Node()
and
Public Property items() As List(Of Node)
and
Public Property items() As NEW List(Of Node)

All with the same results. I am not new to handling JSON, however, I am stumped at this point and figure it is something very subtle.


Solution

  • you have to define a node class this way

    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