我想以JSON解析Open Exchange Rates JSON,和我使用这种方法:JavaScriptSerializer.Deserialize()转换成字典
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);
如果我理解正确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; }
}
我希望能够重播“CurrencyRates率”的东西,如:
public Dictionary<string, decimal> 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
}
}
你能告诉你的JSON的样子? – 2012-02-10 13:47:04
@MichalB。 - 它在上面的链接,它已经添加了一个样本现在的帖子:) – iKode 2012-02-10 14:18:10