无法从JSON查询中获取价值

时间:2014-11-13 07:29:58

标签: c# json

我正在尝试从json api读取数据。我的代码如下,我还包含了一些json数据的片段。我试图在我的例子中获得GBP的当前转换率。我的代码以粗体显示在行中。这是我第一次使用JSON,那么有一种简单的方法可以从这些数据中仅返回GBP的值吗?

public class Currency
    {
        public List<string> rates { get; set; }
    }

    public decimal convertCurrency(decimal amount, string fromCurrency, string toCurrency)
    {
        WebClient client = new WebClient();
        decimal rate = 0;
        string url = String.Format("https://openexchangerates.org/api/latest.json?app_id={0}", ConfigurationManager.AppSettings["OpenExchangeRate_AppID"]);

        using (StreamReader r = new StreamReader(client.OpenRead(url)))
        {
            string json = r.ReadToEnd();
            **List<Currency> items = JsonConvert.DeserializeObject<List<Currency>>(json);**

            foreach (var item in items[0].rates)
            {
                if (item == toCurrency.ToUpper())
                {
                    // currency matches
                    rate = Convert.ToDecimal(item);
                }
            }
        }

        return rate;
    }

{

"disclaimer": "Exchange rates are provided for informational purposes only, and do not constitute financial advice of any kind. Although every attempt is made to ensure quality, NO guarantees are given whatsoever of accuracy, validity, availability, or fitness for any purpose - please use at your own risk. All usage is subject to your acceptance of the Terms and Conditions of Service, available at: https://openexchangerates.org/terms/",   
"license": "Data sourced from various providers with public-facing APIs; copyright may apply; resale is prohibited; no warranties given of any kind. Bitcoin data provided by http://coindesk.com. All usage is subject to your acceptance of the License Agreement available at: https://openexchangerates.org/license/",   
"timestamp": 1415858442,   
"base": "USD",   
"rates": {
"AED": 3.672917,
"AFN": 57.690401,
"GBP": 0.634097   
} 
}

2 个答案:

答案 0 :(得分:2)

public class Currency
    {
        public Rates rates { get; set; }

        public class Rates
        {
            public decimal AED { get; set; }
            public decimal AFN { get; set; }
            public decimal GBP { get; set; }
        }
    }

得到这样的费率

var items = JsonConvert.DeserializeObject<Currency>(json);
        rate = items.rates.GBP;

答案 1 :(得分:1)

为了OP和其他读者的未来利益,有一个在线实用程序,允许您粘贴JSON,它将为您生成C#类 - Jonathan Keith's jsontocsharp

以上JSON的结果与feiyun的答案非常类似(并且在功能上等同于):

输入JSON:

{"disclaimer": "Exchange rates are provided for informational purposes only, and do not constitute financial advice of any kind. Although every attempt is made to ensure quality, NO guarantees are given whatsoever of accuracy, validity, availability, or fitness for any purpose - please use at your own risk. All usage is subject to your acceptance of the Terms and Conditions of Service, available at: https://openexchangerates.org/terms/",   
 "license": "Data sourced from various providers with public-facing APIs; copyright may apply; resale is prohibited; no warranties given of any kind. Bitcoin data provided by http://coindesk.com. All usage is subject to your acceptance of the License Agreement available at: https://openexchangerates.org/license/",   
 "timestamp": 1415858442,   
 "base": "USD",   
 "rates": { "AED": 3.672917,
            "AFN": 57.690401,
            "GBP": 0.634097   
 } 
}

jsontocsharp的输出:

public class Rates
{

    public double AED { get; set; }
    public double AFN { get; set; }
    public double GBP { get; set; }
}

public class RootObject
{

    public string disclaimer { get; set; }
    public string license { get; set; }
    public int timestamp { get; set; }
    public string @base { get; set; }
    public Rates rates { get; set; }
}