JavaScriptSerializer.Deserialize()转换成字典 [英] JavaScriptSerializer.Deserialize() into a dictionary

查看:610
本文介绍了JavaScriptSerializer.Deserialize()转换成字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析开放汇率JSON 在JSON和我用这种方式:

 的HttpWebRequest的WebRequest = GetWebRequest(http://openexchangerates.org/latest.json);

HttpWebResponse响应=(HttpWebResponse)webRequest.GetResponse();
字符串jsonResponse =的String.Empty;
使用(StreamReader的SR =新的StreamReader(response.GetResponseStream()))
{
    jsonResponse = sr.ReadToEnd();
}

无功序列化=新JavaScriptSerializer();
CurrencyRateResponse rateResponse = serializer.Deserialize< CurrencyRateResponse>(jsonResponse);
 

如果我理解正确JavaScriptSerializer.Deserialize我需要定义和对象将JSON转换成。

我可以使用的数据类型像这样成功的序列化:

 公共类CurrencyRateResponse
{
    公共字符串免责声明{获得;组; }
    公共字符串许可证{获得;组; }
    公共字符串时间戳{获得;组; }
    公共字符串basePrice {获得;组; }
    公共CurrencyRates率{获得;组; }
}

公共类CurrencyRates
{
    公共字符串AED {获得;组; }
    公共字符串AFN {获得;组; }
    公共字符串的所有{获得;组; }
    公共字符串AMD {获得;组; }
}
 

我希望能够重播CurrencyRates率的东西,如:

 公共字典<字符串,小数> rateDictionary {获得;组; }
 

但是解析器总是返回rateDictionary为空。任何想法,如果这是可能的,或者你有更好的解决办法?

编辑: 的Json看起来是这样的:

  {
    免责声明:这是声明,
    许可证:数据来自不同供应商提供面向公众的API收集,
    时间戳:1328880864,
    基地:美元,
    利率:{
        AED:3.6731,
        AFN:49.200001,
        ALL:105.589996,
        AMD:388.690002,
        ANG:1.79
    }
}
 

解决方案

这code与您的样本数据。

 公共类CurrencyRateResponse
{
    公共字符串免责声明{获得;组; }
    公共字符串许可证{获得;组; }
    公共字符串时间戳{获得;组; }
    公共字符串@base {获得;组; }
    公共字典<字符串,小数>率{获得;组; }
}

JavaScriptSerializer SER =新JavaScriptSerializer();
VAR OBJ = ser.Deserialize< CurrencyRateResponse>(JSON);
VAR率= obj.rates [AMD];
 

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

解决方案

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

这篇关于JavaScriptSerializer.Deserialize()转换成字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆