最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - JavaScriptSerializer.Deserialize() into a dictionary - Stack Overflow

programmeradmin0浏览0评论

I am trying to parse Open Exchange Rates JSON in Json, and I'm using this approach:

HttpWebRequest webRequest = GetWebRequest(".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);

If I understand the JavaScriptSerializer.Deserialize properly I need to define and object to turn the Json into.

I can successfully serialize it using datatypes like this:

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; }  
} 

I would like to be able to replay "CurrencyRates rates" with something like:

public Dictionary<string, decimal> rateDictionary { get; set; }

but the parser always returns the rateDictionary as null. Any idea if this is possible, or do you have a better solution?

Edit: Json looks like this:

{
    "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
    }
}

I am trying to parse Open Exchange Rates JSON in Json, and I'm using this approach:

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);

If I understand the JavaScriptSerializer.Deserialize properly I need to define and object to turn the Json into.

I can successfully serialize it using datatypes like this:

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; }  
} 

I would like to be able to replay "CurrencyRates rates" with something like:

public Dictionary<string, decimal> rateDictionary { get; set; }

but the parser always returns the rateDictionary as null. Any idea if this is possible, or do you have a better solution?

Edit: Json looks like this:

{
    "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
    }
}
Share Improve this question edited Feb 10, 2012 at 14:17 Positonic asked Feb 10, 2012 at 13:41 PositonicPositonic 9,41114 gold badges58 silver badges87 bronze badges 2
  • Can you show how your json looks like? – Michal B. Commented Feb 10, 2012 at 13:47
  • @MichalB. - sorrry it's on the link above, I have added a sample to the post now :) – Positonic Commented Feb 10, 2012 at 14:18
Add a comment  | 

3 Answers 3

Reset to default 9

If your json is like:

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

then you should be able to do:

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

This compiles:

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

You should be able to figure it out yourself from here.

This code works with your sample data

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"];
    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"]; 
发布评论

评论列表(0)

  1. 暂无评论