1

I need to get a JSON property dynamically at run time. The JSON looks like this:

{
  "timestamp": 1369828868,
  "base": "USD",
  "rates": {
    "AED": 3.673416,
    "AFN": 54.135233,
    "ALL": 108.828249,
    "AMD": 419.878748,
    "ANG": 1.788475,
    "AOA": 96.154668,
    "XDR": 0.66935,
    "XOF": 507.521247,
    "XPF": 92.277412,
    "YER": 214.913206,
    "ZAR": 9.769538,
    "ZMK": 5227.108333,
    "ZMW": 5.316935,
    "ZWL": 322.322775
  }
}

I need to get a currency from the "Rates" array above. I need some help figuring out how to query the JSON structure. I'm using Newtonsoft.

What I wan't to avoid doing is hard coding a switch statement in C#, so I do NOT want to do this:

var json = JsonConvert.DeserializeObject(jsonString) as dynamic;
switch (currencyPair.QuoteCurrencyCode)
{
    case "EUR":
        exchangeRate = json.rates.EUR;
        break;
    case "CNY":
        exchangeRate = json.rates.CNY;
        break;
    case "NZD":
        exchangeRate = json.rates.NZD;
        break;
    case "USD":
        exchangeRate = json.rates.USD;
        break;
    case "GBP":
        exchangeRate = json.rates.GBP;
        break;
    case "HKD":
        exchangeRate = json.rates.HKD;
        break;
    case "JPY":
        exchangeRate = json.rates.JPY;
        break;
    case "CAD":
        exchangeRate = json.rates.CAD;
        break;
    default:
        throw new Exception("Unsupported to currency: " + currencyPair.QuoteCurrencyCode);
}

2 Answers 2

2

You can create a dictionary, using Json.Net

var jObj = JObject.Parse(json);
var rates = jObj["rates"].Children().Cast<JProperty>()
            .ToDictionary(p => p.Name, p => (double)p.Value);

//A single statement instead of switch
var exchangeRate = rates[currencyPair.QuoteCurrencyCode];
Sign up to request clarification or add additional context in comments.

1 Comment

I had to replace JObject with JArray. I attempted to use this solution, but in my scenario I was receiving a set of records and had no root/wrapper object.
0

You can use Json.Net to do this: Example:

            string json = @"{
  ""timestamp"": 1369828868,
  ""base"": ""USD"",
  ""rates"": {
    ""AED"": 3.673416,
    ""AFN"": 54.135233,
    ""ALL"": 108.828249,
    ""AMD"": 419.878748,
    ""ANG"": 1.788475,
    ""AOA"": 96.154668,
    ""XDR"": 0.66935,
    ""XOF"": 507.521247,
    ""XPF"": 92.277412,
    ""YER"": 214.913206,
    ""ZAR"": 9.769538,
    ""ZMK"": 5227.108333,
    ""ZMW"": 5.316935,
    ""ZWL"": 322.322775
  }
}";
            dynamic data = Newtonsoft.Json.JsonConvert.DeserializeObject(json);

            if (data.@base == "USD")
            {

            }
// Get the rates
foreach (var rate in data.rates) { };

Comments

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.