Iterating though desearialised JSON via HttpWebResponse

Home / json / Iterating though desearialised JSON via HttpWebResponse

Question:
In a WPF application we are returning JSON like this
{
"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
}
and it is being deserialised like this
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
The following classes pick up, and work correctly, for the root elements
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
And returned like this….
Dim IsSuccessful As Boolean = ResponseObject.IsSuccessful
Dim vStatusCode As Integer = ResponseObject.StatusCode
Dim vMessage As String = ResponseObject.StatusMessage
I have tried a number of ways to return the sub elements, like adding this to the above class
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)
…and this
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
But this always returns zero
Dim vAccounts As Integer = ResponseObject.AccountNumber.Count
I think the main problem is AccountNumber contains both data and an array and somehow the neurons are not firing with that configuration 🙂

Any pointers would be appreciated…

Thank you


Answer:

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *