2012-02-10 42 views
5

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 
    } 
} 
+0

Bạn có thể hiển thị như thế nào json của bạn trông như thế nào? –

+0

@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

Trả lời

5

Mã này làm việc với dữ liệu mẫu của bạn

public class CurrencyRateResponse 
{ 
    public string disclaimer { get; set; } 
    public string license { get; set; } 
    public string timestamp { get; set; } 
    public string @base { get; set; } 
    public Dictionary<string,decimal> rates { get; set; } 
} 

JavaScriptSerializer ser = new JavaScriptSerializer(); 
var obj = ser.Deserialize<CurrencyRateResponse>(json); 
var rate = obj.rates["AMD"]; 
8

Nếu json bạn cũng giống như:

{"key":1,"key2":2,...} 

sau đó bạn sẽ có thể làm:

Dictionary<string, string> rateDict = serializer.Deserialize<Dictionary<string, string>>(json); 

này biên dịch:

string json = "{\"key\":1,\"key2\":2}"; 
var ser = new System.Web.Script.Serialization.JavaScriptSerializer(); 
var dict = ser.Deserialize<Dictionary<string, int>>(json); 

Bạn sẽ có thể tự mình tìm ra từ đây.

0
Below code will work fine, CurrencyRates is collection so that by using List we can take all reates. 
    This should work!! 

    public class CurrencyRateResponse 
    { 
     public string disclaimer { get; set; } 
     public string license { get; set; } 
     public string timestamp { get; set; } 
     public string basePrice { get; set; }   
     public List<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; } 
    } 

JavaScriptSerializer ser = new JavaScriptSerializer(); 
var obj = ser.Deserialize<CurrencyRateResponse>(json); 
var rate = obj.rates["AMD"]; 
Các vấn đề liên quan