Jamie Keeling
Jamie Keeling

Reputation: 9966

Deserializing json string into an object - Silverlight

I've spent a good while this afternoon trying to implement the deserialization of JSON within a string, at first I was using DataContractJsonSerializer as my environment is Silverlight however it does not appear to support using a Dictionary out of the box (Raised in many other SO questions).

As an alternative I decided to use JSON.NET for the time being (Based on the answers to the aforementioned SO questions) and i've hit the following problem.

I want to deserialize the JSON below:

{
    "disclaimer": "This data is collected from various providers and provided free of charge for informational purposes only, with no guarantee whatsoever of accuracy, validity, availability or fitness for any purpose; use at your own risk. Other than that - have fun, and please share/watch/fork if you think data like this should be free!",
    "license": "Data collected from various providers with public-facing APIs; copyright may apply; not for resale; no warranties given.",
    "timestamp": 1334183999,
    "base": "USD",
    "rates": {
                "AED": 3.6732,
                "AFN": 48.400002,
                "ALL": 106.669998,
             }
}

and place it within the following object (the double within the dictionary is required):

public class ExchangeData
{
    public string disclaimer { get; set; }
    public string license { get; set; }
    public string timestamp { get; set; }
    public string @base { get; set; }
    public Dictionary<string, double> rates { get; set; }
}

My latest attempt at actually getting this to work is below:

StreamReader reader = new StreamReader(args.Result);
ExchangeData data = JsonConvert.DeserializeObject<ExchangeData>(reader.ReadToEnd());

But this results in the following exception:

Could not load type 'System.Dynamic.IDynamicMetaObjectProvider' from assembly 'System.Core, Version=3.7.0.0, Culture=neutral, PublicKeyToken=969DB8053D3322AC'.

Based on what you can see is my approach completely wrong or am I just making a schoolboy error (or both!)

Thanks for your time!

Upvotes: 3

Views: 1962

Answers (3)

Jamie Keeling
Jamie Keeling

Reputation: 9966

The exception message itself appears to be a known problem as raised in this SO question:

Moving to JSON.NET 4.0.3 broke my app

After using Nuget to install the latest package with all necessary dependencies (I manually downloaded the .DLL's from the CodePlex project previously) the code worked with no additional changes.

Thank you to the users who provided solutions.

Upvotes: 1

Erik Philips
Erik Philips

Reputation: 54636

According to your exception: (a simple google search pulled up this answer)

It seems like your project is referencing to an older version of Silverlight runtime.

To check, bring up the project property in Visual Studio, and ensure the Silverlight version is set to 4.0.

You might also want to double check the System.Windows.Controls.Navigation assembly, make sure it's referencing to latest version which usually located in [Program Files]\Microsoft SDKs\Silverlight\v4.0\Libraries\Client\System.Windows.Controls.Navigation.dll

And the following:

"rates": {
            "AED": 3.6732,
            "AFN": 48.400002,
            "ALL": 106.669998,
         }

Is not in JSON, an Array, it is an object. An Array would look like:

"rates": [
            "AED": 3.6732,
            "AFN": 48.400002,
            "ALL": 106.669998,
         ]

So either you have to get the source to properly format it's JSON, or you need to manually setup the deserialization for this specific piece to populate a dictionary.

Upvotes: 0

Tarik
Tarik

Reputation: 81801

I think that would help you:

JavaScriptSerializer ser = new JavaScriptSerializer();
ExchangeData foo = ser.Deserialize<ExchangeData>(args.Result);

I am not really sure you need to use StreamReader, what do you use it anyway?

By the way I assume args.Result is json string.

Upvotes: 1

Related Questions