Convert JSON to a Type

April 6, 2024 ยท View on GitHub

This sample converts LINQ to JSON objects to .NET types using Argon.JToken.ToObject(System.Type).

var v1 = new JValue(true);

var b = (bool) v1.ToObject(typeof(bool));

Console.WriteLine(b);
// true

var i = (int) v1.ToObject(typeof(int));

Console.WriteLine(i);
// 1

var s = (string) v1.ToObject(typeof(string));

Console.WriteLine(s);
// "True"

snippet source | anchor