Complete Novice to JSON - this is my first foray into JSON with C#.
I have a single record returned from an HTTP get request.
var exchRateData = Get($"https://api.exchangeratesapi.io/2018-10-30?base=EUR&symbols=AUD");
Returns :
{"date":"2018-10-30","rates":{"AUD":1.6025},"base":"EUR"}.
The AUD and 1.6025 values are both variables in the GET request. So the GET request can be for SGD or INR in which it returns:
{"date":"2018-10-30","rates":{"SGD":0.0187977737},"base":"INR"}
Always ONE record with levels is returned like those two examples above.
What I would like is to access the values of the second level values,
i.e {"AUD":1.6025} or {"SGD":0.0187977737}
I have tried:
var jsonResult = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(exchRateData);
foreach (var keyValue in jsonResult)
{
try
{
LblGetValues.Text = LblGetValues.Text + "Key: " + keyValue.Key + " Value:" + keyValue.Value + "<br>";
if (keyValue.Key == "date")
LblDate.Text = keyValue.Value;
//This is where I get stuck.
if (keyValue.Key == "rates")
{
// I would like to be able to do this here :
//string currencyCode = "AUD" (or "SGD" if second GET request)
//double currencyRate = 1.6025 (or 0.0187977737 if second GET request)
//JArray secondLevel = new JArray("rates");
// LblRate.Text = keyValue.Value.ToString();
}
if (keyValue.Key == "base")
LblBase.Text = keyValue.Value;
}
catch (Exception errMsg)
{
LblMsg.Text = errMsg.Message + errMsg.StackTrace;
}
}
Please help....
