我正在编写一个程序来访问http://api.fixer.io/latest?base=INR中的货币数据。我能够获取json数据,去除它并将其移动到var obj。
var obj = js.Deserialize<dynamic>(json);
现在我想访问obj中的所有费率,即货币代码和单独字段中的货币值,并更新SQL。我不确定如何在对象上使用foreach或for循环。
我尝试使用以下代码,但它在第二个foreach循环中出错。
foreach (KeyValuePair<string, object> currency in obj)
{
if (currency.Key == "rates")
{
foreach(KeyValuePair<string, double> i in currency.Value)
{
Console.WriteLine("{0} : {1}", currency.Key, currency.Value);
}
}
}
答案 0 :(得分:1)
你不需要第二个foreach。
foreach (KeyValuePair<string, object> currency in obj)
{
if (currency.Key == "rates")
{
Console.WriteLine("{0} : {1}", currency.Key, currency.Value);
}
}
您已找到所需的货币。