C
C#2y ago
point9408

❔ How to convert object type to double. Pls help me

[Route("/")]
public async Task<IActionResult> Index()
{
string stockSymbol = _options.DefaultStockSymbol;

if (string.IsNullOrEmpty(stockSymbol))
{
stockSymbol = "MSFT";
}


Dictionary<string, object> profile = await finnhubService.GetCompanyProfile(stockSymbol); //returns json data
Dictionary<string, object> price = await _finnhubService.GetStockPriceQuote(stockSymbol);

StockTrade stockTrade = new StockTrade
{
StockSymbol = stockSymbol,
StockName = profile.ContainsKey("name") ? profile["name"].ToString() : null,
//Price = price.ContainsKey("c") ? price["c"].GetDouble() : 0.0

};

return View(stockTrade);
}
[Route("/")]
public async Task<IActionResult> Index()
{
string stockSymbol = _options.DefaultStockSymbol;

if (string.IsNullOrEmpty(stockSymbol))
{
stockSymbol = "MSFT";
}


Dictionary<string, object> profile = await finnhubService.GetCompanyProfile(stockSymbol); //returns json data
Dictionary<string, object> price = await _finnhubService.GetStockPriceQuote(stockSymbol);

StockTrade stockTrade = new StockTrade
{
StockSymbol = stockSymbol,
StockName = profile.ContainsKey("name") ? profile["name"].ToString() : null,
//Price = price.ContainsKey("c") ? price["c"].GetDouble() : 0.0

};

return View(stockTrade);
}
In this example from controller, i get value of price as dictionary type and it has many values from external API . The first one is {[c, ValueKind = Number : "246.27"]} which i want to get that double number (246.27) and assign it to my Price variable. You see that I commented that line because it gives error. Is there any other way to achieve this?
13 Replies
Angius
Angius2y ago
Well, object doesn't have a method GetDouble(), so Also, why Dictionary<string, object>? It seems like you're getting properly structured data, but here you go discarding the type system
point9408
point9408OP2y ago
It's just a part of assignment
Angius
Angius2y ago
Ah, so a garbage professor teaching garbage practices
point9408
point9408OP2y ago
xD what can be an alternative way to achieve this?
Angius
Angius2y ago
A class A good and proper class If you get JSON data from somewhere, deserialize it to a class
point9408
point9408OP2y ago
I already did in Service folder and I implement that method in controller
Angius
Angius2y ago
Yeah, deserializing to a dictionary is where the bad code starts Deserialize to a class
point9408
point9408OP2y ago
Oh, okay. I will try it
Angius
Angius2y ago
$jsongen
MODiX
MODiX2y ago
Use https://app.quicktype.io or https://json2csharp.com to generate classes from your JSON
Instantly parse JSON in any language | quicktype
Whether you're using C#, Swift, TypeScript, Go, C++ or other languages, quicktype generates models and helper code for quickly and safely reading JSON in your apps. Customize online with advanced options, or download a command-line tool.
Convert JSON to C# Classes Online - Json2CSharp Toolkit
Convert any JSON object to C# classes online. Json2CSharp is a free toolkit that will help you generate C# classes on the fly.
Angius
Angius2y ago
You can even take the JSON you get from the API and just generate those classes Or you can write them by hand, sure
point9408
point9408OP2y ago
Thank you so much
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server