将JSON数组反序列化为对象列表

时间:2012-07-27 08:33:25

标签: vb.net json deserialization javascriptserializer

我正在尝试使用JavascriptSerializer在.NET中反序列化一些JSON。我正在使用的JSON示例如下:

[{
"dep_date": "2012-07-12", 
"tax": 141.53, 
"currency": "GBP", 
"vcr": "LX", 
"dst_apt": "PRG", 
"flight_in": [
    [
        {
            "depApt": "PRG", 
            "dstApt": "FRA", 
            "depTime": "2012-07-15 19:05:00", 
            "vcr": "LH", 
            "carrier": "LH", 
            "arrTime": "2012-07-15 20:15:00", 
            "ocr": "LH", 
            "flightNo": "1401"
        }, 
        {
            "depApt": "FRA", 
            "dstApt": "LHR", 
            "depTime": "2012-07-15 21:30:00", 
            "vcr": "LH", 
            "carrier": "LH", 
            "arrTime": "2012-07-15 22:10:00", 
            "ocr": "LH", 
            "flightNo": "922"
        }
    ]
], 
"price": 114.0, 
"dst_city": "PRG", 
"dep_apt": "LCY", 
"flight_out": [
    [
        {
            "depApt": "LCY", 
            "dstApt": "ZRH", 
            "depTime": "2012-07-12 08:25:00", 
            "vcr": "LX", 
            "carrier": "LX", 
            "arrTime": "2012-07-12 11:15:00", 
            "ocr": "LX", 
            "flightNo": "451"
        }, 
        {
            "depApt": "ZRH", 
            "dstApt": "PRG", 
            "depTime": "2012-07-12 12:35:00", 
            "vcr": "LX", 
            "carrier": "LX", 
            "arrTime": "2012-07-12 13:55:00", 
            "ocr": "2L", 
            "flightNo": "1486"
        }
    ]
], 
"ret_date": "2012-07-15", 
}]

我正在使用的实际代码/类是:

<Serializable()> _
    Public Class FareResult

        Public Property dep_date As String
        Public Property tax As String
        Public Property currency As String
        Public Property vcr As String
        Public Property dst_apt As String
        Public Property flight_in As List(Of FlightResult)
        Public Property price As String
        Public Property dst_city As String
        Public Property dep_apt As String
        Public Property flight_out As List(Of FlightResult)
        Public Property ret_date As String

    End Class

    <Serializable()> _
    Public Class FlightResult

        Public Property depApt As String
        Public Property dstApt As String
        Public Property depTime As String
        Public Property vcr As String
        Public Property carrier As String
        Public Property arrTime As String
        Public Property ocr As String
        Public Property flightNo As String

    End Class

Dim jss As New JavaScriptSerializer
Dim oFareResults As Generic.List(Of FareResult) = jss.Deserialize(Of List(Of FareResult))(sJSON)

但是,这只是给我一条消息,说明不支持类型反序列化的FlightResults类型。我已经尝试创建一个继承FlightResults列表的类,我尝试将其设置为数组而不是列表,但它们都给出了相同的异常。

我在这里错过了什么吗?

1 个答案:

答案 0 :(得分:0)

flight_inflight_out更改为数组(最后请注意())。

Public Property flight_in As List(Of FlightResult)()
Public Property flight_out As List(Of FlightResult)()

您的JSON有一些解析错误(通过validator运行)。请注意在结尾附近删除逗号。这是一个有效的固定副本。

[{
"dep_date": "2012-07-12", 
"tax": 141.53, 
"currency": "GBP", 
"vcr": "LX", 
"dst_apt": "PRG", 
"flight_in": [
    [
        {
            "depApt": "PRG", 
            "dstApt": "FRA", 
            "depTime": "2012-07-15 19:05:00", 
            "vcr": "LH", 
            "carrier": "LH", 
            "arrTime": "2012-07-15 20:15:00", 
            "ocr": "LH", 
            "flightNo": "1401"
        }, 
        {
            "depApt": "FRA", 
            "dstApt": "LHR", 
            "depTime": "2012-07-15 21:30:00", 
            "vcr": "LH", 
            "carrier": "LH", 
            "arrTime": "2012-07-15 22:10:00", 
            "ocr": "LH", 
            "flightNo": "922"
        }
    ]
], 
"price": 114.0, 
"dst_city": "PRG", 
"dep_apt": "LCY", 
"flight_out": [
    [
        {
            "depApt": "LCY", 
            "dstApt": "ZRH", 
            "depTime": "2012-07-12 08:25:00", 
            "vcr": "LX", 
            "carrier": "LX", 
            "arrTime": "2012-07-12 11:15:00", 
            "ocr": "LX", 
            "flightNo": "451"
        }, 
        {
            "depApt": "ZRH", 
            "dstApt": "PRG", 
            "depTime": "2012-07-12 12:35:00", 
            "vcr": "LX", 
            "carrier": "LX", 
            "arrTime": "2012-07-12 13:55:00", 
            "ocr": "2L", 
            "flightNo": "1486"
        }
    ]
], 
"ret_date": "2012-07-15"
}]