3

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!

Jamie Keeling
  • 9,806
  • 17
  • 65
  • 102

3 Answers3

1

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.

Tarik
  • 79,711
  • 83
  • 236
  • 349
  • From what I understand the JavaScriptSerializer is not available in Silverlight (as mentioned in my question) which is why in using JSON.NET - I'm open to alternatives if there are any. Additionally args.Result is a Stream. – Jamie Keeling Apr 12 '12 at 00:02
1

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.

Community
  • 1
  • 1
Jamie Keeling
  • 9,806
  • 17
  • 65
  • 102
0

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.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • This was due to an issue with using the .DLL (and has since been rectified in a later version). Using the code I posted in my questions the JSON was deserialized correctly, including the rates. Thank you for your answer either way. – Jamie Keeling Apr 12 '12 at 08:05