Trying to parse/deserialize JSON array with no key?
I have the following JSON format:
Code:
[{
"type":"deposit",
"currency":"btc",
"amount":"0.0",
"available":"0.0"
},{
"type":"deposit",
"currency":"usd",
"amount":"1.0",
"available":"1.0"
},{
"type":"exchange",
"currency":"btc",
"amount":"1",
"available":"1"
},{
"type":"exchange",
"currency":"usd",
"amount":"1",
"available":"1"
},{
"type":"trading",
"currency":"btc",
"amount":"1",
"available":"1"
},{
"type":"trading",
"currency":"usd",
"amount":"1",
"available":"1"
}]
I'm trying to return some of the "available" and "amount" values, but cannot get it to work for the life of me. I created the following class:
Code:
Public Class ItemsReturned
Public Property type As String
Public Property currency As String
Public Property amount As String
Public Property available As String
End Class
I'm then using
Code:
Dim balances As ItemsReturned = JsonConvert.DeserializeObject(Of ItemsReturned)(TheJson)
but this gives me the error "cannot deserialize the current JSON array because the type requires a JSON object"
Re: Trying to parse/deserialize JSON array with no key?
Try:
Code:
Dim balances() As ItemsReturned = JsonConvert.DeserializeObject(Of ItemsReturned())(TheJson)
where balances() is an array of ItemsReturned.
Re: Trying to parse/deserialize JSON array with no key?
Quote:
Originally Posted by
Inferrd
Try:
Code:
Dim balances() As ItemsReturned = JsonConvert.DeserializeObject(Of ItemsReturned())(TheJson)
where balances() is an array of ItemsReturned.
I think that worked. So how would I select say the second "amount" value?
Re: Trying to parse/deserialize JSON array with no key?
Quote:
Originally Posted by
hockeyadc
I think that worked. So how would I select say the second "amount" value?
Arrays are zero based, so it would be :
Code:
Dim theAmount As String = balances(1).amount
Re: Trying to parse/deserialize JSON array with no key?
AH yes thats it. For some reason I was screwing up the array and class syntax. Thank you! :thumb: