hopeless bob
hopeless bob

Reputation: 15

vb json deserialize method causing errors

I receive this json string from a web server

 [
        {
        "rt_id": "1",
        "rt_lat": "-37.773654",
        "rt_lng": "175.009544",
        "rt_prox": "500",
        "rt_direction": "180",
        "rt_avoiddays": "0",
        "rt_validfrom": null,
        "rt_validto": null,
        "rt_gefid": "15345346",
        "rt_description": "this is the text for an alert of nature REALTIME",
        "rt_expiry": "274834873"
    },
    {
        "rt_id": "5",
        "rt_lat": "-37.773945",
        "rt_lng": "175.275208",
        "rt_prox": "500",
        "rt_direction": null,
        "rt_avoiddays": null,
        "rt_validfrom": null,
        "rt_validto": null,
        "rt_gefid": null,
        "rt_description": "congestion yes another note about congestion",
        "rt_expiry": null
    },
    {
        "rt_id": "9",
        "rt_lat": "-37.764954",
        "rt_lng": "175.303314",
        "rt_prox": "200",
        "rt_direction": null,
        "rt_avoiddays": null,
        "rt_validfrom": null,
        "rt_validto": null,
        "rt_gefid": null,
        "rt_description": "other this is a new road",
        "rt_expiry": null
    },
    {
        "rt_id": "10",
        "rt_lat": "-37.787045",
        "rt_lng": "175.280960",
        "rt_prox": "50",
        "rt_direction": null,
        "rt_avoiddays": null,
        "rt_validfrom": null,
        "rt_validto": null,
        "rt_gefid": null,
        "rt_description": "only park frontwards in these parks. You may be fined for reversing into the parking spaces.",
        "rt_expiry": null
    }
]

I am trying my hardest to deserialize it and into my class called realtime, which is made up like so

<Serializable>
Public Class realtime


    Dim m_rt_lng As String
    Dim m_rt_direction As String
    Dim m_rt_prox As String
    Dim m_rt_validfrom As String
    Dim m_rt_avoiddays As String
    Dim m_rt_lat As String
    Dim m_rt_description As String
    Dim m_rt_id As String
    Dim m_rt_validto As String
    Dim m_rt_gefid As String
    Dim m_rt_expiry As String



    Public Property rt_lng() As String
        Get
            Return m_rt_lng
        End Get
        Set(value As String)
            m_rt_lng = value
        End Set
    End Property


    Property rt_direction As String
        Get
            Return m_rt_direction
        End Get
        Set(ByVal Value As String)
            m_rt_direction = Value
        End Set
    End Property

    Property rt_prox As String
        Get
            Return m_rt_prox
        End Get
        Set(ByVal Value As String)
            m_rt_prox = Value
        End Set
    End Property

    Property rt_validfrom As String
        Get
            Return m_rt_validfrom
        End Get
        Set(ByVal Value As String)
            m_rt_validfrom = Value
        End Set
    End Property

    Property rt_avoiddays As String
        Get
            Return m_rt_avoiddays
        End Get
        Set(ByVal Value As String)
            m_rt_avoiddays = Value
        End Set
    End Property

    Property rt_lat As String
        Get
            Return m_rt_lat
        End Get
        Set(ByVal Value As String)
            m_rt_lat = Value
        End Set
    End Property

    Property rt_description As String
        Get
            Return m_rt_description
        End Get
        Set(ByVal Value As String)
            m_rt_description = Value
        End Set
    End Property

    Property rt_id As String
        Get
            Return m_rt_id
        End Get
        Set(ByVal Value As String)
            m_rt_id = Value
        End Set
    End Property

    Property rt_validto As String
        Get
            Return m_rt_validto
        End Get
        Set(ByVal Value As String)
            m_rt_validto = Value
        End Set
    End Property


    Property rt_gefid As String
        Get
            Return m_rt_gefid
        End Get
        Set(ByVal Value As String)
            m_rt_gefid = Value
        End Set
    End Property




End Class

I can see the json string in the msg box but get the following error: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'openroadDBadmin.realtime' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly. This is the vb code I'm using

Sub getalldatafromrealtime()
        Dim req As HttpWebRequest = HttpWebRequest.Create("http://xxxxxxxxxxxxxx/realtime.php?minlat=-40.77394&maxlat=-34.00&minlng=174.000&maxlng=176.000")
        Dim re As HttpWebResponse = req.GetResponse()
        Dim src As String = New System.IO.StreamReader(re.GetResponseStream()).ReadToEnd()

        MsgBox(src)
        Dim xrealtime As realtime = Newtonsoft.Json.JsonConvert.DeserializeObject(Of realtime)(src)
        MsgBox(xrealtime)
    End Sub

I have looked at at least 10 examples, the only thing I can think of is the null values are causing issues, I have tried various libraries for deserializing json and json lint seems to like the json structure.

any ideas would be highly appreciated

I eventually just want to get this into a datagridview or table, before I turn 90

Upvotes: 0

Views: 217

Answers (1)

hopeless bob
hopeless bob

Reputation: 15

solved, for the benefit of other readers : This occurred because the json array was stored in a list by the php script code modified to: deserialiseObject**(OF LIST**(

Sub getalldatafromrealtime()

        Dim req As HttpWebRequest = HttpWebRequest.Create("http://xxxxxxxs.com/subfolder/script.php?minlat=-40.77394&maxlat=-34.00&minlng=174.000&maxlng=176.000")
        Dim re As HttpWebResponse = req.GetResponse()
        Dim src As String = New System.IO.StreamReader(re.GetResponseStream()).ReadToEnd()


        rtlist = Newtonsoft.Json.JsonConvert.DeserializeObject(OF LIST(Of realtime))(src)
        MsgBox(src)

        MsgBox(rtlist.Item(0).rt_description)
        addtodatagridview()

    End Sub
    Sub addtodatagridview()
        DataGridView1.DataSource = rtlist


    End Sub

Upvotes: 1

Related Questions