0

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.

jayesh mhatre
  • 77
  • 1
  • 1
  • 6
  • Please show your code which fetches the values so that one is able to see which library (if any) you are using and can provide an appropriate example in that way – DAXaholic Jul 12 '16 at 05:37
  • JToken token = JObject.Parse(response.Content); ActualCurrencies.Swiss_Franc = (string)token.SelectToken("From.CHF.AsAtDate1"); – jayesh mhatre Jul 12 '16 at 05:52
  • Need somthing to fetch in dictionary or string array list to fetch Keys values – jayesh mhatre Jul 12 '16 at 05:54
  • Dupe to http://stackoverflow.com/questions/6522358/how-can-i-get-a-list-of-keys-from-json-net – kurakura88 Jul 12 '16 at 06:02

1 Answers1

3

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
Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • Im using IRestResponse in place of jsonText – jayesh mhatre Jul 12 '16 at 06:37
  • @jayeshmhatre just get the josn string out of the response and proceed with the above logic. – Zein Makki Jul 12 '16 at 06:38
  • @jayeshmhatre `string jsonText = response.Content;` – Zein Makki Jul 12 '16 at 06:41
  • Error 15 'System.Collections.Generic.Dictionary.KeyCollection' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'System.Collections.Generic.Dictionary.KeyCollection' could be found (are you missing a using directive or an assembly reference?) C:\Users\mhatrejv\Desktop\CoreAnalytics.TestFramework sample project\CoreAnalytics.TestFramework\EMLAPIData.cs 370 50 CoreAnalytics.TestFramework.WebAPI – jayesh mhatre Jul 12 '16 at 06:57
  • @jayeshmhatre add `using System.Linq;` at the top of your file – Zein Makki Jul 12 '16 at 07:00