vb json反序列化方法导致错误

时间:2014-06-17 16:10:56

标签: json vb.net serialization

我从网络服务器

收到这个json字符串
 [
        {
        "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
    }
]

我正在尽最大努力将其反序列化并进入我的班级,称为realtime,它就是这样构成的

<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

我可以在msg框中看到json字符串 但是得到以下错误: 无法将当前JSON数组(例如[1,2,3])反序列化为类型“openroadDBadmin.realtime”,因为该类型需要一个JSON对象(例如{“name”:“value”})来反序列化正确。 这是我正在使用的vb代码

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

我已经查看了至少10个示例,我唯一能想到的是null值导致问题,我已经尝试了各种库来反序列化json和json lint似乎喜欢json结构。

任何想法都将受到高度赞赏

我最终只想把它变成数据网格视图或表格,然后才转过90

1 个答案:

答案 0 :(得分:1)

解决了,为了其他读者的利益: 发生这种情况是因为json数组由php脚本存储在列表中 代码修改为: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