JavaScriptSerializer.Deserialize() into a dictionary

18,489

Solution 1

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"];

Solution 2

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.

Share:
18,489
iKode
Author by

iKode

Updated on June 08, 2022

Comments

  • iKode
    iKode almost 2 years

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