About the author
I had to retrieve JSON data and parse the information in C#, as I need to retrieve bitcoin rates between different bitcoin operators.
A quick research brings me to a Stackoverflow question on how to achieve this.
Retrieving the JSON data from the bitcoin operator URL, I got the following raw data.
{"ask":"760.00","bid":"720.00"}
Following the Stackoverflow question, I quickly coded up the following routine which is what I learnt in a few minutes. Basically, the trick is to declare a structure class which contains the same field names as the JSON object, and I'm done! Oh wait, not so fast! I have to download the JSON library first. For that, I went to James Newton-King's page and got it.
class BitcoinRate { public double ask; public double bid; } class Program { private const float ValueInMyCurrency = 1.0f; // Actual value different static void Main(string[] args) { using (WebClient webClient = new System.Net.WebClient()) { String json = webClient.DownloadString(URL); BitcoinRate tmp = JsonConvert.DeserializeObject<BitcoinRate>(json); double bitcoinValue = ValueInMyCurrency / tmp.bid; bitcoinValue = Math.Round(bitcoinValue, 3, MidpointRounding.AwayFromZero)+0.001; } } }
Started up Visual Studio, and hurray, the code is correct!
You can also use one of the established solutions such as: github.com/.../BitcoinLib for this purpose and get the value straight from the bitcoin network thus not depending on a 3rd party service which might actually provide you with inaccurate info at times.
Thanks for the pointer, George.
Due to different rates in different geographical regions, retrieving the exchange rate from the Bitcoin network will not work for me.