Search code examples
jsonvb.nethttpjavascriptserializer

JSON deserilaization of single string in JSON


this is my JSON string I'm receive from my HTTP response.

[{"ts":"1395318389","date":"2014-03-20","time":"12:26:21","nodeid":"1229001363","lat":"53.292425","lon":"-6.43203333333333","speed":"76","dir":"242","sv":"9","volt":"4187","rssi":"-69","us":"3","type":0,"net":"27202","height":"108","hdop":"9","cellid":"EB84","dd":6105,"ttf":"0","lac":"4E24","odo":"0","gle":"0"}]

My class is set up like this

Friend Class SmartTrack_PostionList
    Public positionList As SmartTrack_Postion()
End Class

Friend Class SmartTrack_Postion
    Public ts As String
    Public [date] As String
    Public time As String
    Public nodeid As String
    Public lat As String
    Public lon As String
    Public speed As String
    Public dir As String
    Public sv As String
    Public volt As String
    Public rssi As String
    Public us As String
    Public type As String
    Public net As String
    Public height As String
    Public hdop As String
    Public cellid As String
    Public dd As String
    Public ttf As String
    Public lac As String
    Public odo As String
    Public gle As String
End Class

I've tried the following lines of code but none are working

Dim ser As New System.Web.Script.Serialization.JavaScriptSerializer
Dim getPositionList As SmartTrack_PostionList = DirectCast(ser.Deserialize(Of SmartTrack_PostionList)(getResult.ToString), SmartTrack_PostionList)

and

Dim ser As New System.Web.Script.Serialization.JavaScriptSerializer
Dim getPositionList As SmartTrack_Postion= DirectCast(ser.Deserialize(Of SmartTrack_Postion)(getResult.ToString), SmartTrack_Postion)

This did work but I think it's bad coding.

Dim ser As New System.Web.Script.Serialization.JavaScriptSerializer
getResult = getResult.Replace("[", "")
getResult = getResult.Replace("]", "")
Dim dict As Dictionary(Of String, String) = ser.Deserialize(Of Dictionary(Of String, String))(getResult)

I would appreciate any help on this, thanks.

Jim


Solution

  • You'll have to deserialize your JSON string into a List(Of SmartTrack_Position), like

    Dim ser = New System.Web.Script.Serialization.JavaScriptSerializer
    Dim json = getResult.ToString()
    Dim getPositionList = ser.Deserialize(Of List(Of SmartTrack_Postion))(json)