bendecko
bendecko

Reputation: 2783

JSON string to VB.net Object with nested list

I've got some JSON coming from a webservice looks like this:

{
  "disclaimer": "Exchange r..",
  "license": "Data sourced from variou..",
  "timestamp": 1262365200,
  "base": "USD",
  "rates": {
    "AED": 3.67275,
    "AFN": 48.550089,
    "ALL": 96.435505,
    "AMD": 377.894224,
    "ANG": 1.791,
    "AOA": 89.174867,
    "ARS": 3.79928
  }
}

I've built a little class to accept it.

   Class currencyValues

        Class ratePairs
            Property currencyCode
            Property currencyValue
        End Class

        Property disclaimer
        Property license
        Property timestamp
        Property base
        Property rates As New List(Of ratePairs)

    End Class

When I run the code to accept the JSON into the class it takes the top level properties, but the list of ratePairs does not populate.

   Dim js As New System.Web.Script.Serialization.JavaScriptSerializer()
    Dim recs = js.Deserialize(Of currencyValues)(curRecordJSON)

The count of list recs.Rates is zero.

What am I doing wrong?

Upvotes: 1

Views: 979

Answers (1)

James Thorpe
James Thorpe

Reputation: 32202

The rates property in the original json is not an array, and therefore can't be deserialised to a list.

It is in fact an object, with properties such as AED and AFN etc. You are able to deserialise it as a Dictionary(Of String, Double), or if the properties never change, you could build a class to hold it:

Class Rates
  Property AED
  Property AFN
  'etc
End Class

Upvotes: 1

Related Questions