使用JavaScriptSerializer反序列化JSON字典列表

时间:2013-12-10 03:55:04

标签: json vb.net serialization

我一直在使用反序列化一些JSON,并且在调试时我在我的立即窗口中收到以下错误:“System.Web.Extensions.dll中出现'System.InvalidOperationException'类型的第一次机会异常”

这是我试图反序列化的JSON:

{ "Asks":[ [ 919, 11.62026440 ], [ 919.06, 0.07269375 ] ], "Bids":[ [ 901.44, 0.01000050 ], [ 901, 0.25000000 ] ] }

我的课程:

<Serializable()>
Public Class orderbookJsonDeserializerClass

    Private _asks As List(Of Dictionary(Of Double, Double))
    Public Property asks() As List(Of Dictionary(Of Double, Double))
        Get
            Return _asks
        End Get
        Set(ByVal value As List(Of Dictionary(Of Double, Double)))
            _asks = value
        End Set
    End Property

    Private _bids As List(Of Dictionary(Of Double, Double))
    Public Property bids() As List(Of Dictionary(Of Double, Double))
        Get
            Return _bids
        End Get
        Set(ByVal value As List(Of Dictionary(Of Double, Double)))
            _bids = value
        End Set
    End Property
End Class

我的代码:

Dim js As New JavaScriptSerializer
Dim testObj = js.Deserialize(Of orderbookJsonDeserializerClass)(responseFromServer)

调试时,我在尝试执行“Dim testObj = js.Deserialize(Of orderbookJsonDeserializerClass)(responseFromServer)”指令时得到前面提到的错误。

1 个答案:

答案 0 :(得分:0)

所以我觉得我这么做太复杂了。在创建了两个对象的类,然后尝试不同的组合之后,我终于找到了正确的类来将JSON反序列化为:

<Serializable()>
Public Class genericOrderbook
    Public Asks As New List(Of List(Of Double))
    Public Bids As New List(Of List(Of Double))
End Class

现在我可以循环并获得所有出价和问题,以及所有爵士乐。问题解决了!