Results 1 to 5 of 5

Thread: Trying to parse/deserialize JSON array with no key?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2013
    Posts
    76

    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"

  2. #2
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    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.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2013
    Posts
    76

    Re: Trying to parse/deserialize JSON array with no key?

    Quote Originally Posted by Inferrd View Post
    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?

  4. #4
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Trying to parse/deserialize JSON array with no key?

    Quote Originally Posted by hockeyadc View Post
    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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2013
    Posts
    76

    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!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width