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.

4
  • 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 Commented Jul 12, 2016 at 5:37
  • JToken token = JObject.Parse(response.Content); ActualCurrencies.Swiss_Franc = (string)token.SelectToken("From.CHF.AsAtDate1"); Commented Jul 12, 2016 at 5:52
  • Need somthing to fetch in dictionary or string array list to fetch Keys values Commented Jul 12, 2016 at 5:54
  • Dupe to stackoverflow.com/questions/6522358/… Commented Jul 12, 2016 at 6:02

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

5 Comments

Im using IRestResponse in place of jsonText
@jayeshmhatre just get the josn string out of the response and proceed with the above logic.
@jayeshmhatre string jsonText = response.Content;
Error 15 'System.Collections.Generic.Dictionary<string,CoreAnalytics.TestFramework.WebAPI.CurrencyRate>.KeyCollection' does not contain a definition for 'ToList' and no extension method 'ToList' accepting a first argument of type 'System.Collections.Generic.Dictionary<string,CoreAnalytics.TestFramework.WebAPI.CurrencyRate>.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
@jayeshmhatre add using System.Linq; at the top of your file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.