1

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?

2
  • The rates in the 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 might be able to deserialise it as a Dictionary(Of String, Double) Commented Dec 19, 2014 at 11:30
  • @JamesThorpe That's it! Oooh you are good. Please post as answer and I will accept. Commented Dec 19, 2014 at 12:14

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

These are currencies, I'm guessing they probably will be more, or less (e.g. Euro merge for new member states) and change over time so the Dictionary solution is the best for me. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.