Consider this code:
JObject obj = JObject.Parse(json);
Dictionary<string,object> dict = new Dictionary<string, object>();
List<string> parameters = new List<string>{"Car", "Truck", "Van"};
foreach (var p in parameters)
{
JToken token = obj.SelectToken(string.Format("$.results[?(@.paramName == '{0}')]['value']", p));
dict[p] = token.Value<object>();
}
string jsonOutput = JsonConvert.SerializeObject(dict);
where json
contains, in part:
{
"paramName": "Car",
"value": 68.107
},
{
"paramName": "Truck",
"value": 48.451
},
{
"paramName": "Van",
"value": 798300
}
While debugging this, I inspected the dictionary and found the values were not of type object
but actual numeric types like integer
and float
. Since the dictionary was declared as Dictionary<string, object> dict = new Dictionary<string, object>();
I expected the values to be of type object
and that I would need to cast them upon use.
The JSON output string is {"Car":68.107,"Truck":48.451,"Van":798300}
.
How does the dictionary know the type of the value and why do I get the actual type instead of the base type object
?
When you call
JsonConvert.SerializeObject(dict);
this takes an object, which then figures out the type, and stores it accordingly.
Therefore for each item in the dictionary entry. it first checks the type and then deserializes it according to its type.
If you wanted to use the object outside of JSON, you would have to check yourself with something like
var o = dict["Car"];
if(o is int) return (int)o; // or whatever you want to do with an int
if(o is decimal) return (decimal)o; // or whatever you want to do with an decimal