2012-07-27 71 views
0

我想在.NET中使用JavascriptSerializer反序列化一些JSON。我正在使用的JSON的例子如下:Deserialise 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列表的類,我試着將它設置爲一個數組而不是列表,但它們都給出了相同的異常。

我在這裏錯過了什麼嗎?

回答

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

不幸的是,這似乎沒有做任何事情。我在一個名爲SGEN的文件中得到了大約25個錯誤,「無法打開,它已被重命名,刪除或移動」。不完全確定爲什麼。將其更改爲flight_in()List(Of FlightResult)也不起作用。 – user1546281 2012-07-27 11:19:54

+1

@ user1546281您的JSON有一些解析錯誤。我用有效的JSON更新了答案。試試看。它*確實*解決了這個問題。 – ThinkingStiff 2012-07-27 17:16:47