2012-08-10 48 views
0

我是新來這個話題,基本上我有這個JSON字符串:JavaScriptSerializer例外

"{"payChannleResult":{"lastestPayChannel":"","supportedPayChannelList":{"supportTopPayChannel":[{"name":"FastPay","cashierCode":"CREDITCARD","supportSecPayChannelList":{"supportSecPayChannel":[{"name":"BankA","cashierCode":"CREDITCARD_A"},{"name":"BankB","cashierCode":"CREDITCARD_B"}, 
{"name":"BankE","cashierCode":"CREDITCARD_E"}, 
{"name":"More","cashierCode":"CREDITCARD"}]}}, 
{"name":"Credits","cashierCode":"DEBITCARD","supportSecPayChannelList":{"supportSecPayChannel":[{"name":"BankF","cashierCode":"DEBITCARD_F"}, 
{"name":"BankI","cashierCode":"DEBITCARD_I"}, 
{"name":"More","cashierCode":"DEBITCARD"}]}}]}}}" 

我有類此:

<Serializable()> Public Class RootObject 
    Private _payChannelResult As PayChannleResult 
    Public Property payChannleResult() As PayChannleResult 
     Get 
      Return _payChannelResult 
     End Get 
     Set(ByVal value As PayChannleResult) 
      _payChannelResult = value 
     End Set 
    End Property 
End Class 

<Serializable()> Public Class PayChannleResult 
    Private _lastestPayChannel As String 'LastestPayChannel 
    Private _supportedPayChannelList As SupportedPayChannelList 

    Public Property lastestPayChannel() As String 
     Get 
      Return _lastestPayChannel 
     End Get 
     Set(ByVal value As String) 
      _lastestPayChannel = value 
     End Set 
    End Property 

    Public Property supportedPayChannelList() As SupportedPayChannelList 
     Get 
      Return _supportedPayChannelList 
     End Get 
     Set(ByVal value As SupportedPayChannelList) 
      _supportedPayChannelList = value 
     End Set 
    End Property 
End Class 

<Serializable()> Public Class SupportedPayChannelList 
    Private _supportTopPayChannel As SupportTopPayChannel 
    Public Property supportTopPayChannel() As SupportTopPayChannel 
     Get 
      Return _supportTopPayChannel 
     End Get 
     Set(ByVal value As SupportTopPayChannel) 
      _supportTopPayChannel = value 
     End Set 
    End Property 
End Class 


<Serializable()> Public Class SupportTopPayChannel 
    Private _name As String 
    Private _cashierCode As String 
    Private _supportSecPayChannelList As SupportSecPayChannelList 

    Public Property name() As String 
     Get 
      Return _name 
     End Get 
     Set(ByVal value As String) 
      _name = value 
     End Set 
    End Property 
    Public Property cashierCode() As String 
     Get 
      Return _cashierCode 
     End Get 
     Set(ByVal value As String) 
      _cashierCode = value 
     End Set 
    End Property 

    Public Property supportSecPayChannelList() As SupportSecPayChannelList 
     Get 
      Return _supportSecPayChannelList 
     End Get 
     Set(ByVal value As SupportSecPayChannelList) 
      _supportSecPayChannelList = value 
     End Set 
    End Property 
End Class 

<Serializable()> Public Class SupportSecPayChannelList 

    Private _supportSecPayChannel As List(Of SupportSecPayChannel) 
    Public Property supportSecPayChannel() As List(Of SupportSecPayChannel) 
     Get 
      Return _supportSecPayChannel 
     End Get 
     Set(ByVal value As List(Of SupportSecPayChannel)) 
      _supportSecPayChannel = value 
     End Set 
    End Property 
End Class 

<Serializable()> Public Class SupportSecPayChannel 
    Private _name As String 
    Private _cashierCode As String 

    Public Property name() As String 
     Get 
      Return _name 
     End Get 
     Set(ByVal value As String) 
      _name = value 
     End Set 
    End Property 
    Public Property cashierCode() As String 
     Get 
      Return _cashierCode 
     End Get 
     Set(ByVal value As String) 
      _cashierCode = value 
     End Set 
    End Property 
End Class 



<Serializable()> Public Class LastestPayChannel 
    Private _name As String 
    Private _cashierCode As String 
    Public Property name() As String 
     Get 
      Return _name 
     End Get 
     Set(ByVal value As String) 
      _name = value 
     End Set 
    End Property 
    Public Property cashierCode() As String 
     Get 
      Return _cashierCode 
     End Get 
     Set(ByVal value As String) 
      _cashierCode = value 
     End Set 
    End Property 
End Class 

最後,這是我怎麼叫解串器功能:

Dim seriliazer As JavaScriptSerializer = New JavaScriptSerializer 
Dim p As RootObject = seriliazer.Deserialize(Of RootObject)(strJson) 

但它給錯誤: 類型 'SupportTopPayChannel' 不是增刊orted用於數組的反序列化。

請大家幫幫我,非常感謝!!!!!

+0

我使用,但不知何故它在CV期間丟失。 – JohnZ 2012-08-10 15:07:32

+0

請發佈一個可重複使用的小例子。 – asawyer 2012-08-10 15:11:08

+0

喜歡?........................ – JohnZ 2012-08-10 15:21:15

回答

0

它看起來像你的VB類定義不太正確。 SupportedPayChannelList類具有未聲明爲列表的屬性。使用下面的類定義(使用List(Of added))。如果您還沒有,請確保具有Imports System.Collections.Generic。

 
Public Class SupportedPayChannelList 
    Private _supportTopPayChannel As List(Of SupportTopPayChannel) 
    Public Property supportTopPayChannel() As List(Of SupportTopPayChannel) 
     Get 
      Return _supportTopPayChannel 
     End Get 
     Set(ByVal value As List(Of SupportTopPayChannel)) 
      _supportTopPayChannel = value 
     End Set 
    End Property 
End Class