I am trying to Parse Json in C# . (I am beginner in Json)
WebRequest webRequest = WebRequest.Create(url);
var httpWebRequest = (HttpWebRequest)webRequest;
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
String responseText = "";
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
responseText = streamReader.ReadToEnd();
}
//
This is Output in responseText :
{
"disclaimer": "Exchange rates 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 made of accuracy, validity, availability, or fitness for any purpose. All usage subject to acceptance of Terms: https://openexchangerates.org/terms/",
"license": "Data sourced from various providers; resale prohibited; no warranties given of any kind. All usage subject to License Agreement: https://openexchangerates.org/license/",
"timestamp": 1454497211,
"base": "EUR",
"rates": {
"INR": 74.42686146,
"USD": 1.0929332
}
}
To get the base from Json
var obj = JObject.Parse(responseText);
String bcur = (string)obj["base"];
Console.WriteLine("base :"bcur.ToString());
Output : base :EUR
similarly to get Rates of INR , I wrote
var obj = JObject.Parse(responseText);
String rte= (string)obj["INR"];
Console.WriteLine("rate:"rte);
it gives me null.
Can anyone tell me what is wrong in my code. or a better way to get the base and rate of the currency .
.ToString()when you are writing out to console when you already specified the type of the variable as a string?+between your string and your variable. Or alternatively, if you are using .Net 4.6 you can use the string interpolation stuff by prefixing your string with a $. Using one of your examplesConsole.WriteLine("rate:"rte);would look likeConsole.WriteLine($"rate:{rte}");