Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScriptSerializer.Deserialize() into a dictionary

I am trying to parse Open Exchange Rates JSON in Json, and I'm using this approach:

HttpWebRequest webRequest = GetWebRequest("http://openexchangerates.org/latest.json");

HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
string jsonResponse = string.Empty;
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
    jsonResponse = sr.ReadToEnd();
}

var serializer = new JavaScriptSerializer();
CurrencyRateResponse rateResponse = serializer.Deserialize<CurrencyRateResponse>(jsonResponse);

If I understand the JavaScriptSerializer.Deserialize properly I need to define and object to turn the Json into.

I can successfully serialize it using datatypes like this:

public class CurrencyRateResponse
{
    public string disclaimer  { get; set; }
    public string license { get; set; }
    public string timestamp { get; set; }
    public string basePrice { get; set; }        
    public CurrencyRates rates { get; set; }
}

public class CurrencyRates
{
    public string AED  { get; set; }    
    public string AFN  { get; set; }    
    public string ALL  { get; set; }    
    public string AMD  { get; set; }  
} 

I would like to be able to replay "CurrencyRates rates" with something like:

public Dictionary<string, decimal> rateDictionary { get; set; }

but the parser always returns the rateDictionary as null. Any idea if this is possible, or do you have a better solution?

Edit: Json looks like this:

{
    "disclaimer": "this is the disclaimer",
    "license": "Data collected from various providers with public-facing APIs",
    "timestamp": 1328880864,
    "base": "USD",
    "rates": {
        "AED": 3.6731,
        "AFN": 49.200001,
        "ALL": 105.589996,
        "AMD": 388.690002,
        "ANG": 1.79
    }
}
like image 662
iKode Avatar asked Feb 10 '12 13:02

iKode


People also ask

What does JavaScriptSerializer do?

The JavaScriptSerializer class is used internally by the asynchronous communication layer to serialize and deserialize the data that is passed between the browser and the Web server.

How do you deserialize an object in C#?

NET objects (deserialize) A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.

Can Dictionary be serialized in C#?

The serialization and deserialization of . NET objects is made easy by using the various serializer classes that it provides. But serialization of a Dictionary object is not that easy. For this, you have to create a special Dictionary class which is able to serialize itself.

What is JavaScriptSerializer in vb net?

Serialization (in VB.NET). SERIALIZATION. For serialization purpose the JavaScriptSerializer has the following method: - Serialize – this method serializes an object and converts it to a JSON string. This means, when we need to serialize a Dictionary, we will need to convert it to object type.


1 Answers

If your json is like:

{"key":1,"key2":2,...}

then you should be able to do:

Dictionary<string, string> rateDict = serializer.Deserialize<Dictionary<string, string>>(json);

This compiles:

string json = "{\"key\":1,\"key2\":2}";
var ser = new System.Web.Script.Serialization.JavaScriptSerializer();
var dict = ser.Deserialize<Dictionary<string, int>>(json);

You should be able to figure it out yourself from here.

like image 161
Michal B. Avatar answered Sep 22 '22 02:09

Michal B.