Tôi cố gắng để phân tích Open Exchange Rates JSON trong Json, và tôi đang sử dụng phương pháp này:JavaScriptSerializer.Deserialize() vào một cuốn từ điển
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);
Nếu tôi hiểu đúng JavaScriptSerializer.Deserialize tôi cần phải xác định và đối tượng để biến Json thành.
tôi thành công có thể serialize nó sử dụng kiểu dữ liệu như thế này:
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; }
}
Tôi muốn để có thể phát lại "giá CurrencyRates" với một cái gì đó như:
public Dictionary<string, decimal> rateDictionary { get; set; }
nhưng phân tích cú pháp luôn luôn trả về rateDictionary là null. Bất kỳ ý tưởng nếu điều này là có thể, hoặc bạn có một giải pháp tốt hơn?
Edit: Json trông như thế này:
{
"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
}
}
Bạn có thể hiển thị như thế nào json của bạn trông như thế nào? –
@MichalB. - sorrry nó trên liên kết ở trên, tôi đã thêm một mẫu vào bài viết bây giờ :) – iKode