JSON中单个字符串的JSON反序列化

时间:2014-03-20 12:56:23

标签: json vb.net http javascriptserializer

这是我从HTTP响应中收到的JSON字符串。

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

我的课程设置如下

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

我尝试了以下几行代码,但没有一行正常工作

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

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

这确实有效,但我认为编码很糟糕。

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)

感谢您的任何帮助,谢谢。

吉姆

2 个答案:

答案 0 :(得分:1)

您必须将JSON字符串反序列化为List(Of SmartTrack_Position),例如

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

答案 1 :(得分:0)

使用此课程

 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; }
}

然后像这样使用json.net

   RootObject Json= JsonConvert.DeserializeObject<RootObject>(returnJson);