Próbuję analizować Open Exchange Rates JSON w JSON i używam tego podejścia:JavaScriptSerializer.Deserialize() do słownika
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);
Jeśli Rozumiem JavaScriptSerializer.Deserialize właściwie muszę zdefiniować i przedmiot do zmień Jsona w.
mogę skutecznie szeregować go za pomocą typów danych tak:
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; }
}
Chciałbym, aby móc odtworzyć „CurrencyRates stopy” z czymś takim:
public Dictionary<string, decimal> rateDictionary { get; set; }
ale parser zawsze zwraca rateDictionary jako null. Masz pomysł, czy jest to możliwe, czy masz lepsze rozwiązanie?
Edit: Json wygląda następująco:
{
"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
}
}
można pokazać, jak wygląda Twój json? –
@MichalB. - sorrry to na powyższym linku, dodałem już próbkę do postu :) – iKode