2012-02-10 11 views
5

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

można pokazać, jak wygląda Twój json? –

+0

@MichalB. - sorrry to na powyższym linku, dodałem już próbkę do postu :) – iKode

Odpowiedz

5

Ten kod działa z danymi przykładowymi

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

Jeśli json jest jak:

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

to powinieneś być w stanie to zrobić:

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

ten kompiluje:

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

Powinieneś być w stanie to rozgryźć.

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"];