2012-02-10 5 views
5

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

¿Puede mostrar cómo su JSON parece? –

+0

@MichalB. - Sorrry está en el enlace de arriba, he añadido una muestra a la publicación ahora :) – iKode

Respuesta

5

Este código funciona con los datos de la muestra

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

Si su JSON es como:

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

, entonces debería ser capaz de hacer:

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

Este compila:

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

Debería poder descifrarlo usted mismo desde aquí.

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