虽然通过HttpWebResponse迭代了JSON

时间:2017-08-18 15:34:45

标签: json wpf

在WPF应用程序中,我们像这样返回JSON

{
"AccountHistory":[
{
"AccountNumber":123456,
"DailyEndingBalances":
[
{"BalanceDate":"\/Date(14508540000000000)\/","EndingBalance":2511.5700},
//Other elements
],
"Transactions":[
{
"TransactionID":"98787676",
//Other elements
},
 {
"TransactionID":"2683901",
 //Other elements
 }],
 "IsSuccessful":true,
 "StatusCode":0,
"StatusMessage":"success"
],
"IsSuccessful":true,
"StatusCode":0,
"StatusMessage":"success
}

它正在被反序列化

Using response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
            Dim deserializer As New DataContractJsonSerializer(GetType(ABank.AHistoryResponse))
            ResponseObject = DirectCast(deserializer.ReadObject(response.GetResponseStream()), ABank.AHistoryResponse)
        End Using

以下类为根元素

选取并正常工作
Public Property IsSuccessful() As Boolean
        Get
            Return m_IsSuccessful
        End Get
        Set
            m_IsSuccessful = Value
        End Set
    End Property
    Private m_IsSuccessful As Boolean

    Public Property StatusCode() As Integer
        Get
            Return m_StatusCode
        End Get
        Set
            m_StatusCode = Value
        End Set
    End Property
    Private m_StatusCode As Integer

    Public Property StatusMessage() As String
        Get
            Return m_StatusMessage
        End Get
        Set
            m_StatusMessage = Value
        End Set
    End Property
    Private m_StatusMessage As String

像这样回来......

 Dim IsSuccessful As Boolean = ResponseObject.IsSuccessful
        Dim vStatusCode As Integer = ResponseObject.StatusCode
        Dim vMessage As String = ResponseObject.StatusMessage

我尝试了很多方法来返回子元素,比如将它添加到上面的类

 Public Sub New()
        m_AccountNumber = New List(Of AccountHistoryAccountNumber)
        m_Transactions = New List(Of AccountHistoryTransactions)
    End Sub
    Public Property AccountNumber As List(Of AccountHistoryAccountNumber)
        Get
            Return m_AccountNumber
        End Get
        Set(value As List(Of AccountHistoryAccountNumber))
            m_AccountNumber = value
        End Set
    End Property
    Private m_AccountNumber As List(Of AccountHistoryAccountNumber)

......还有这个

Public Class AccountHistoryAccountNumber

    Public Property AccountNumber() As Long
        Get
            Return m_AccountNumber
        End Get
        Set
            m_AccountNumber = Value
        End Set
    End Property
    Private m_AccountNumber As Long

    Public Property StatusMessage() As String
        Get
            Return m_StatusMessage
        End Get
        Set
            m_StatusMessage = Value
        End Set
    End Property
    Private m_StatusMessage As String

    Public Property IsSuccessful() As Boolean
        Get
            Return m_IsSuccessful
        End Get
        Set
            m_IsSuccessful = Value
        End Set
    End Property
    Private m_IsSuccessful As Boolean

    Public Property StatusCode() As Integer
        Get
            Return m_StatusCode
        End Get
        Set
            m_StatusCode = Value
        End Set
    End Property
    Private m_StatusCode As Integer


End Class

但这总是返回零

Dim vAccounts As Integer = ResponseObject.AccountNumber.Count

我认为主要问题是AccountNumber包含数据和数组,不知何故神经元没有使用该配置触发: - )

任何指针都会受到赞赏......

谢谢

1 个答案:

答案 0 :(得分:0)

最后,我发现了一个非常有用的实用工具http://json2csharp.com/,它将JSON转换为C#类......

我使用Fiddler捕获了传入的原始JSON,将其粘贴到实用程序中,并且能够获得正确的类结构

 Public Class RootObject

    Public Property AccountHistory() As List(Of AccountHistory)
        Get
            Return m_AccountHistory
        End Get
        Set(value As List(Of AccountHistory))
            m_AccountHistory = value
        End Set
    End Property
    Private m_AccountHistory As List(Of AccountHistory)

    Public Sub New()
        m_AccountHistory = New List(Of AccountHistory)
    End Sub
    Public Property IsSuccessful() As Boolean
        Get
            Return m_IsSuccessful
        End Get
        Set
            m_IsSuccessful = Value
        End Set
    End Property
    Private m_IsSuccessful As Boolean

    Public Property StatusCode() As Integer
        Get
            Return m_StatusCode
        End Get
        Set
            m_StatusCode = Value
        End Set
    End Property
    Private m_StatusCode As Integer

    Public Property StatusMessage() As String
        Get
            Return m_StatusMessage
        End Get
        Set
            m_StatusMessage = Value
        End Set
    End Property
    Private m_StatusMessage As String
End Class


Public Class AccountHistory

    Public Property AccountNumber() As Long
        Get
            Return m_AccountNumber
        End Get
        Set(value As Long)
            m_AccountNumber = value
        End Set
    End Property
    Private m_AccountNumber As Long

    Public Sub New()
        m_Transactions = New List(Of Transactions)
    End Sub

    Public Property Transactions() As List(Of Transactions)
        Get
            Return m_Transactions
        End Get
        Set(value As List(Of Transactions))
            m_Transactions = value
        End Set
    End Property

    Private m_Transactions As List(Of Transactions)

    Public Property IsSuccessful() As Boolean
        Get
            Return m_IsSuccessful
        End Get
        Set
            m_IsSuccessful = Value
        End Set
    End Property
    Private m_IsSuccessful As Boolean

    Public Property StatusCode() As Integer
        Get
            Return m_StatusCode
        End Get
        Set
            m_StatusCode = Value
        End Set
    End Property
    Private m_StatusCode As Integer

    Public Property StatusMessage() As String
        Get
            Return m_StatusMessage
        End Get
        Set
            m_StatusMessage = Value
        End Set
    End Property
    Private m_StatusMessage As String

End Class

Public Class Transactions

    Public Property TransactionID() As String
        Get
            Return m_TransactionID
        End Get
        Set(value As String)
            m_TransactionID = value
        End Set
    End Property
    Private m_TransactionID As String

    Public Property PostedDate() As String
        Get
            Return m_PostedDate
        End Get
        Set(value As String)
            m_PostedDate = value
        End Set
    End Property
    Private m_PostedDate As String

    Public Property CheckNumber() As String
        Get
            Return m_CheckNumber
        End Get
        Set(value As String)
            m_CheckNumber = value
        End Set
    End Property
    Private m_CheckNumber As String

    Public Property TraceNumber() As Long
        Get
            Return m_TraceNumber
        End Get
        Set(value As Long)
            m_TraceNumber = value
        End Set
    End Property

    Private m_TraceNumber As Long

End Class