Positonic
Positonic

Reputation: 9411

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
    }
}

Upvotes: 5

Views: 19757

Answers (3)

Sunil Shrikhande
Sunil Shrikhande

Reputation: 1

    Below code will work fine, CurrencyRates is collection so that by using List we can take all reates.
    This should work!!

    public class CurrencyRateResponse
    {
        public string disclaimer  { get; set; }
        public string license { get; set; }
        public string timestamp { get; set; }
        public string basePrice { get; set; }        
        public List<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; }  
    }

JavaScriptSerializer ser = new JavaScriptSerializer();
var obj =  ser.Deserialize<CurrencyRateResponse>(json);
var rate = obj.rates["AMD"]; 

Upvotes: 0

L.B
L.B

Reputation: 116178

This code works with your sample data

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

JavaScriptSerializer ser = new JavaScriptSerializer();
var obj =  ser.Deserialize<CurrencyRateResponse>(json);
var rate = obj.rates["AMD"];

Upvotes: 5

Michal B.
Michal B.

Reputation: 5719

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.

Upvotes: 9

Related Questions