C# JavaScriptSerializer.Deserialize()到字典中
我正在尝试用Json进行解析,并使用以下方法:C# JavaScriptSerializer.Deserialize()到字典中,c#,javascript,.net,json,deserialization,C#,Javascript,.net,Json,Deserialization,我正在尝试用Json进行解析,并使用以下方法: HttpWebRequest webRequest = GetWebRequest("http://openexchangerates.org/latest.json"); HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse(); string jsonResponse = string.Empty; using (StreamReader sr = new Str
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);
我希望能够用以下内容重播“CurrencyRates”:
public Dictionary<string, decimal> rateDictionary { get; set; }
如果您的json类似于:
{"key":1,"key2":2,...}
那么你应该能够做到:
Dictionary<string, string> rateDict = serializer.Deserialize<Dictionary<string, string>>(json);
Dictionary rateDict=serializer.Deserialize(json);
这包括:
string json = "{\"key\":1,\"key2\":2}";
var ser = new System.Web.Script.Serialization.JavaScriptSerializer();
var dict = ser.Deserialize<Dictionary<string, int>>(json);
string json=“{\'key\':1,\'key2\':2}”;
var ser=new System.Web.Script.Serialization.JavaScriptSerializer();
var dict=ser.Deserialize(json);
您应该能够从这里自己计算出来。此代码适用于您的示例数据
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"];
公共类CurrencyRateResponse
{
公共字符串免责声明{get;set;}
公共字符串许可证{get;set;}
公共字符串时间戳{get;set;}
公共字符串@base{get;set;}
公共字典率{get;set;}
}
JavaScriptSerializer ser=新的JavaScriptSerializer();
var obj=ser.Deserialize(json);
var率=目标利率[“AMD”];
下面的代码可以正常工作,CurrencyRates是一个集合,因此通过使用列表,我们可以获取所有创建。
这应该管用!!
公共类CurrencyRateResponse
{
公共字符串免责声明{get;set;}
公共字符串许可证{get;set;}
公共字符串时间戳{get;set;}
公共字符串基价{get;set;}
公共列表比率{get;set;}
}
公共类货币汇率
{
公共字符串AED{get;set;}
公共字符串AFN{get;set;}
公共字符串ALL{get;set;}
公共字符串AMD{get;set;}
}
JavaScriptSerializer ser=新的JavaScriptSerializer();
var obj=ser.Deserialize(json);
var率=目标利率[“AMD”];
你能展示一下你的json是什么样子吗?@MichalB.-对不起,在上面的链接上,我现在在帖子中添加了一个示例:)
string json = "{\"key\":1,\"key2\":2}";
var ser = new System.Web.Script.Serialization.JavaScriptSerializer();
var dict = ser.Deserialize<Dictionary<string, int>>(json);
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"];