estoy tratando de analizar Open Exchange Rates JSON en JSON, y estoy usando este enfoque:JavaScriptSerializer.Deserialize() en un diccionario
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);
Si entiendo correctamente la JavaScriptSerializer.Deserialize I necesidad de definir y objeto a enciende el Json.
Puedo serializar con éxito el uso de tipos de datos como esto:
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; }
}
me gustaría ser capaz de jugar de nuevo "tasas CurrencyRates" con algo como:
public Dictionary<string, decimal> rateDictionary { get; set; }
pero el analizador siempre devuelve el rateDictionary como nulo. Alguna idea si esto es posible, o tienes una mejor solución?
Editar: JSON se ve así:
{
"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
}
}
¿Puede mostrar cómo su JSON parece? –
@MichalB. - Sorrry está en el enlace de arriba, he añadido una muestra a la publicación ahora :) – iKode