2

I'm creating an ASP.Net core MVC application to retrieve and display current currencies with their values in a user friendly way on my html page. I am using cryptocompare's api to obtain these values.

The Json response I get from the api is

{"EUR":{"BTC":0.0003188,"USD":1.14}}

EUR.cs

namespace ProjectFinanceHub.Models
{

    public class EUR
    {
        public double BTC { get; set; }
        public double USD { get; set; }
    }

}

HomeController.cs Below I have removed my personal api key and replaced it 'your_api_here'

namespace ProjectFinanceHub.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";

        return View();
    }

    public async Task<ActionResult> EUR()
    {
        var req = WebRequest.Create(@"https://min-api.cryptocompare.com/data/pricemulti?fsyms=EUR&tsyms=BTC,USD,EUR&api_key=your_api_here");
        var r = await req.GetResponseAsync().ConfigureAwait(false);

        var responseReader = new StreamReader(r.GetResponseStream());
        var responseData = await responseReader.ReadToEndAsync();

        var d = Newtonsoft.Json.JsonConvert.DeserializeObject<EUR>(responseData);
        return View(d);
     }
    }
 }

EUR.cshtml

@model ProjectFinanceHub.Models.EUR

<h2>@Model.BTC</h2> <!-- Should display 0.0003188 for example-->
<h3>@Model.USD</h3> <!-- Should display 1.13 for example-->

Currently I get values of 0 display for both BTC and USD, the expected values are that these values are the ones from the json response

ekad
  • 14,436
  • 26
  • 44
  • 46
ciara walsh
  • 33
  • 1
  • 5

2 Answers2

4

You need to wrap EUR with a root element

public class EUR
{
    public double BTC { get; set; }
    public double USD { get; set; }
}

public class RootObject
{
    public EUR EUR { get; set; }
}

DeserializeObject object needs to be modified to

var d = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(responseData);

Please ensure your return d.EUR.

return View(d.EUR);
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
  • thank you for your response but I got this message when I loaded the page after making your changes. Server Error in '/' Application. The model item passed into the dictionary is of type 'ProjectFinanceHub.Models.RootObject', but this dictionary requires a model item of type 'ProjectFinanceHub.Models.EUR'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. – ciara walsh Jan 21 '19 at 17:33
  • You need return View(d. EUR) – Anu Viswan Jan 21 '19 at 17:35
1

What fixed my problem was a mixed solution between implementing the changes @Anu pointed out and also thanks to https://stackoverflow.com/a/17788118/9864898 I reconfigured my controller as such

public async Task<ActionResult> EUR()
    {
        // output is {"EUR":{"BTC":0.0003188,"USD":1.13,"EUR":1}} for the below api at time of request
        var req = WebRequest.Create(@"https://min-api.cryptocompare.com/data/pricemulti?fsyms=EUR&tsyms=BTC,USD&api_key=your_api_key");
        var r = await req.GetResponseAsync().ConfigureAwait(false);

        var responseReader = new StreamReader(r.GetResponseStream());
        var responseData = new JsonTextReader(responseReader);

        var serializer = new JsonSerializer();

        var d = serializer.Deserialize<RootObject>(responseData);
        return View(d.EUR);
    }
John-Luke Laue
  • 3,736
  • 3
  • 32
  • 60
ciara walsh
  • 33
  • 1
  • 5