【问题标题】:JSON deserilaization of single string in JSONJSON中单个字符串的JSON反序列化
【发布时间】:2014-03-20 12:56:22
【问题描述】:

这是我从 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)

如果能提供任何帮助,我将不胜感激,谢谢。

吉姆

【问题讨论】:

    标签: json vb.net http javascriptserializer


    【解决方案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)
    

    【讨论】:

    • 谢谢 Dominic 我将代码更改为 Dim ser As New System.Web.Script.Serialization.JavaScriptSerializer Dim getPositionList As List(Of SmartTrack_Postion) = ser.Deserialize(Of List(Of SmartTrack_Postion))( getResult.ToString) 完美!
    【解决方案2】:

    使用这个类

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

    【讨论】:

    • 哦,如果你没有安装它,你可能需要通过 NUGET json.net
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2021-09-02
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多