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

5
  • Have you checked the var d value while debugging? Commented Jan 21, 2019 at 16:44
  • 1
    Have you seen this SO answer? stackoverflow.com/questions/8157636/… Commented Jan 21, 2019 at 16:45
  • Can you edit your question to show a sample Json response? Commented Jan 21, 2019 at 16:45
  • Possible duplicate of Can Json.NET serialize / deserialize to / from a stream? Commented Jan 21, 2019 at 16:51
  • @ste-fu yes! This is the issue. Commented Jan 21, 2019 at 16:53

2 Answers 2

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

2 Comments

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.
You need return View(d. EUR)
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);
    }

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.