Question

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

Was it helpful?

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)

OTHER TIPS

Use this class

 public class RootObject
{
public string ts { get; set; }
public string date { get; set; }
public string time { get; set; }
public string nodeid { get; set; }
public string lat { get; set; }
public string lon { get; set; }
public string speed { get; set; }
public string dir { get; set; }
public string sv { get; set; }
public string volt { get; set; }
public string rssi { get; set; }
public string us { get; set; }
public int type { get; set; }
public string net { get; set; }
public string height { get; set; }
public string hdop { get; set; }
public string cellid { get; set; }
public int dd { get; set; }
public string ttf { get; set; }
public string lac { get; set; }
public string odo { get; set; }
public string gle { get; set; }
}

Then use json.net like this

   RootObject Json= JsonConvert.DeserializeObject<RootObject>(returnJson);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top