IMRUP
IMRUP

Reputation: 1513

Get value from json result returned by web.DownloadString(uri);

I have api for converting currency

WebClient web = new WebClient();
                Uri uri = new Uri(string.Format("https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency={0}&to_currency={1}&apikey=demo", currFrom, currTo));
                string response = web.DownloadString(uri);

response string contains

{
    "Realtime Currency Exchange Rate": {
        "1. From_Currency Code": "USD",
        "2. From_Currency Name": "United States Dollar",
        "3. To_Currency Code": "INR",
        "4. To_Currency Name": "Indian Rupee",
        "5. Exchange Rate": "71.44300000",
        "6. Last Refreshed": "2019-02-19 10:01:45",
        "7. Time Zone": "UTC"
    }
}

how i will get exchange rate from this ?

Please help me

Upvotes: 0

Views: 35

Answers (1)

Andrei Dragotoniu
Andrei Dragotoniu

Reputation: 6335

for that particular response, you could use something like this:

    string response = "{\"Realtime Currency Exchange Rate\": {\"1. From_Currency Code\": \"USD\",\"2. From_Currency Name\": \"United States Dollar\",\"3. To_Currency Code\": \"INR\",\"4. To_Currency Name\": \"Indian Rupee\",\"5. Exchange Rate\": \"71.44300000\",\"6. Last Refreshed\": \"2019-02-19 10:01:45\",\"7. Time Zone\": \"UTC\"}}";

var parsedObject = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(response);

var actualContent = parsedObject["Realtime Currency Exchange Rate"];
var currencyCode = actualContent.ElementAt(0);
var currencyName = actualContent.ElementAt(1);
.........
var exchangeRate = actualContent.ElementAt(4);

All you need is a reference to Newtonsoft.Json basically

Upvotes: 1

Related Questions