Removing json string deserialization into an object - Silverlight

I had a good day trying to implement JSON deserialization inside a string, at first I used DataContractJsonSerializer , since my Silverlight environment, however, does not seem to support using Dictionary out of the box (raised in many other SO issues).

As an alternative, I decided to use JSON.NET for the moment (based on the answers to the above SO questions), and I applied the following problem.

I want to deserialize 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 put it in the following object (double dictionary 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 last attempt to actually get this to work below:

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

But this leads to the following exception:

'System.Dynamic.IDynamicMetaObjectProvider' 'System.Core, Version = 3.7.0.0, Culture = neutral, PublicKeyToken = 969DB8053D3322AC'.

, , ( , !)

!

+3
3

, SO:

JSON.NET 4.0.3

Nuget ( .DLL CodePlex ) - .

, .

+1

, :

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

, StreamReader, ?

, , args.Result - json.

+1

: ( Google )

, Silverlight.

, Visual Studio , Silverlight 4.0.

System.Windows.Controls.Navigation, , , [Program Files]\Microsoft SDK\Silverlight\v4.0\Libraries\Client\System.Windows.Controls.Navigation.dll

:

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

JSON, Array, . :

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

So, either you need the source to format the JSON correctly, or you need to manually configure the deserialization for this specific part to populate the dictionary.

0
source

All Articles