javascript JavaScriptSerializer.Deserialize() 成字典

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9228671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:59:43  来源:igfitidea点击:

JavaScriptSerializer.Deserialize() into a dictionary

c#javascript.netjsondeserialization

提问by iKode

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

我正在尝试用 Json解析Open Exchange Rates JSON,我正在使用这种方法:

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.

如果我正确理解 JavaScriptSerializer.Deserialize,我需要定义和对象以将 Json 转换为。

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:

我希望能够使用以下内容重播“CurrencyRates rate”:

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?

但解析器总是将 rateDictionary 返回为 null。知道这是否可行,或者您有更好的解决方案吗?

Edit: Json looks like this:

编辑: Json 看起来像这样:

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

回答by Michal B.

If your json is like:

如果你的 json 是这样的:

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

你应该能够从这里自己弄清楚。

回答by L.B

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

回答by Sunil Shrikhande

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