【问题标题】:Deserialise JSON array to list of object将 JSON 数组反序列化为对象列表
【发布时间】:2012-07-27 08:33:25
【问题描述】:

我正在尝试使用 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 列表的类,我尝试将其设置为数组而不是列表,但它们都给出了相同的异常。

我错过了什么吗?

【问题讨论】:

    标签: vb.net json deserialization javascriptserializer


    【解决方案1】:

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

    【讨论】:

    • 不幸的是,这似乎没有任何作用。我在一个名为 SGEN 的文件中收到大约 25 个错误,即“无法打开。它已被重命名、删除或移动”。不完全确定为什么。将其更改为 flight_in() As List(Of FlightResult) 也不起作用。
    • @user1546281 您的 JSON 有一些解析错误。我用有效的 JSON 更新了答案。试试看。它确实解决了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    相关资源
    最近更新 更多