JavaScriptSerializer.Deserialize () в словаре

Я пытаюсь проанализировать Open Exchange Rates JSON в Json, и я использую такой подход:

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(jsonResponse);

Если я понимаю JavaScriptSerializer.Deserialize правильно, мне нужно определить и объект, чтобы превратить Json в.

Я могу успешно сериализовать его, используя такие типы данных:

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

Я хотел бы иметь возможность воспроизвести «Курсы валют» с чем-то вроде:

public Dictionary rateDictionary { get; set; }

, но синтаксический анализатор всегда возвращает rateDictionary как null. Есть идеи, возможно ли это, или у вас есть лучшее решение?

Изменить: Json выглядит так:

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

5
задан iKode 10 February 2012 в 14:17
поделиться