Step 1: As the first stage, for analyzing (processing) the JSON files, I constitude the NeWTON library as follows:
Step 2: Add Newtonsoft.Json reference in “using”.
1 2 3 4 5 |
using System; using System.Net; using Newtonsoft.Json; |
http://www.floatrates.com/json-feeds.html
I choose ” Dollar” because I want to convert dollars into other currencies
http://www.floatrates.com/daily/usd.json
1 2 3 4 5 |
string url = "http://www.floatrates.com/daily/usd.json"; string json = new WebClient().DownloadString(url); var currency = JsonConvert.DeserializeObject<dynamic>(json); |
Printing “currency” object will be sufficient for seeing all currencies
1 2 3 |
Console.WriteLine(currency); |
To Read the Euro currency rate, you must write the first line of following code (currency.eur.rate).
if you also want to convert Euro to Dollar ,you must write the second line of following code (currency.eur.inverseRate).
1 2 3 4 |
Console.WriteLine(currency.eur.rate); Console.WriteLine(currency.eur.inverseRate); |
Sample Application 1: If we want to show the code in a simple console application, we can do the following.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
using System; using System.Net; using Newtonsoft.Json; namespace currencyexample { class Program { static void Main(string[] args) { double amount, curAmount; int exchangeType; string url = "http://www.floatrates.com/daily/usd.json"; string json = new WebClient().DownloadString(url); var currency = JsonConvert.DeserializeObject<dynamic>(json); Console.Write("Enter Your Amount:"); amount = Convert.ToSingle(Console.ReadLine()); Console.Write("\nPress(1) for Dollar->Euro \nPress(2) for Euro->Dollar \nEnter exchange type: "); exchangeType = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("*************************"); if(exchangeType==1) { curAmount = amount * Convert.ToSingle(currency.eur.rate); Console.WriteLine("{0:N2}{1}", curAmount,currency.eur.code); } else if(exchangeType == 2) { curAmount = amount * Convert.ToSingle(currency.eur.inverseRate); Console.WriteLine("{0:N2}{1}", curAmount, "USD"); } Console.ReadLine(); } } } |
Sample Application 1: The same example created with another JSON API
For Dollar to Euro : http://api.openrates.io/latest?base=USD
For Euro to Dollar : http://api.openrates.io/latest?base=EUR
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using System; using System.Net; using Newtonsoft.Json; namespace currencyexample { class Program { static void Main(string[] args) { double amount, curAmount; string url = "http://api.openrates.io/latest?base=USD"; string json = new WebClient().DownloadString(url); var currency = JsonConvert.DeserializeObject<dynamic>(json); Console.Write("Enter Your Amount:"); amount = Convert.ToSingle(Console.ReadLine()); Console.WriteLine("*************************"); curAmount = amount * Convert.ToSingle(currency.rates.EUR); Console.WriteLine("{0:N2} {1} = {2:N2} {3}",amount, currency["base"],curAmount,"EUR"); Console.ReadLine(); } } } |