Search code examples
c#json.netcoinbase-api

Unable to get child value on newtonsoft.Json.LinqJvalue when reading coinbase api?


Hello I am trying to get the coinbase API exchange rates from the URL however json.net does not seem to be able to format the JSON file to read the children enter image description here

this is the method that throws the error

public string CalculateCoinValue(Coinbase connector)
{
    //bool valid = connector.rates.isValid;

    string cryptoExchangeRate;

    if (connector.rates != null)
    {
        cryptoExchangeRate = (string)connector.rates["data"]["currency"]["rates"][Settings.defualtCrypto.ToUpper()];

    }

    return "";
}

this is the method im using to get the JSON data which stores it in a JObject whithin the Coinbase class

public async Task<JObject> GetExchangeRatesAsync()
{

    HttpClient webClient = new HttpClient();
    string url = string.Format("https://api.coinbase.com/v2/exchange-rates?currency=" + Settings.defaultCurrency);
    HttpResponseMessage coinbaseJsonRaw = await webClient.GetAsync(url);
    return JObject.Parse(await coinbaseJsonRaw.Content.ReadAsStringAsync());
}

what am I doing wrong for this not to work?


Solution

  • The json returned looks different. try the below

    cryptoExchangeRate = (string)connector["data"]["rates"][Settings.defualtCrypto.ToUpper()];