I've tried:
JValue myJValue = getJValue(someVar);
int storedValue = JsonConvert.DeserializeObject(myJValue);
But this only seems to be valid for JObjects. Is there a way to get the integer from a JValue?
Maybe this helps you along:
int storedValue = myJValue.ToObject<int>();
For anyone interested in Performance, Value()
is much much quicker than ToObject()
. For strings, just use ToString()
Int Test:
value.Value<int>() - 2496ms
value.ToObject<int>() - 6259ms
Double Test:
value.Value<double>() - 572ms
value.ToObject<double>() - 6319ms
String Test:
value.Value<string>() - 1767ms
value.ToObject<string>() - 6768ms
value.ToString() - 130ms
.
private static void RunPerfTest()
{
int loops = 100000000;
JValue value = new JValue(1000d);
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < loops; i++)
{
double x = value.Value<double>();
}
sw.Stop();
Console.WriteLine("value.Value<double>()" + sw.ElapsedMilliseconds);
sw.Restart();
for (int i = 0; i < loops; i++)
{
double x = value.ToObject<double>();
}
sw.Stop();
Console.WriteLine("value.ToObject<double>()" + sw.ElapsedMilliseconds);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With