Currency Converter
dotnet add package Newtonsoft.Json --version 13.0.18. Buat fungsi getRate yang berfungsi untuk mendapatkan kurs mata uang dari api diatas. Fungsi ini akan menggunakan WebClient serta JsonConvert dari package Newtonsoft tadi. Berikut fungsinya
private double getRate(string fromCurrency, string toCurrency) { var json = ""; string rate = ""; try { string url = string.Format("https://free.currconv.com/api/v7/convert?q={0}_{1}&compact=ultra&apiKey=[YOUR_API_KEY]", fromCurrency.ToUpper(), toCurrency.ToUpper()); string key = string.Format("{0}_{1}", fromCurrency.ToUpper(), toCurrency.ToUpper()); json = new WebClient().DownloadString(url); dynamic stuff = JsonConvert.DeserializeObject(json); rate = stuff[key]; } catch { rate = "0"; } return double.Parse(rate);
Komentar
Posting Komentar