❔ How to convert object type to double. Pls help me
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
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 systemIt's just a part of assignment
Ah, so a garbage professor teaching garbage practices
xD
what can be an alternative way to achieve this?
A class
A good and proper class
If you get JSON data from somewhere, deserialize it to a class
I already did in Service folder and I implement that method in controller
Yeah, deserializing to a dictionary is where the bad code starts
Deserialize to a class
Oh, okay. I will try it
$jsongen
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.
You can even take the JSON you get from the API and just generate those classes
Or you can write them by hand, sure
Thank you so much
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.