Reputation: 87
JSON :
{"From":
{"CHF":{"Rate":0.91640105,"AsAtDate":"2016-04-19T00:00:00"},
"DKK":{"Rate":0.13437824,"AsAtDate":"2016-04-19T00:00:00"},
"EUR":{"Rate":1.0,"AsAtDate":"2016-04-19T00:00:00"},
"GBP":{"Rate":1.25985769,"AsAtDate":"2016-04-19T00:00:00"},
"PLN":{"Rate":0.23213581,"AsAtDate":"2016-04-19T00:00:00"},
"RON":{"Rate":0.22338218,"AsAtDate":"2016-04-19T00:00:00"},
"SEK":{"Rate":0.10868521,"AsAtDate":"2016-04-19T00:00:00"}},
"To":"EUR","RequestedDate":"2016-07-08T00:00:00"}
I want to fetch list of keys under from. Eg. list should return keys all values like DKK, EUR,GBP . C# code is required to deserialize JSON.
I am able to fetch values from JSON but not keys.
Upvotes: 0
Views: 397
Reputation: 30062
You can create classes to match your json Format, and then read the keys:
public class CurrecyConversion
{
public Dictionary<string, CurrencyRate> From { set; get; }
public string To { set; get; }
public DateTime RequestedDate { set; get; }
}
public class CurrencyRate
{
public decimal Rate { set; get; }
public DateTime AsAtDate { set; get; }
}
You need to download this Nuget Package (Right Click Project > Manager Nuget Packages)
Usage:
CurrecyConversion result = Newtonsoft.Json.JsonConvert.
DeserializeObject<CurrecyConversion>(jsonText);
List<string> keys = result.From.Keys.ToList();
foreach (var key in keys)
Console.WriteLine(key);
Result:
CHF
DKK
EUR
GBP
PLN
RON
SEK
Upvotes: 3