What is the difference between the JToken.ToObject<T>()
method and the JToken.Value<T>()
extension method (the one without the key
parameter)?
var jToken = JToken.Parse("123");
var toObjectStrResult = jToken.ToObject<string>();
var valueStrResult = jToken.Value<string>();
// toObjectStrResult == valueStrResult == "123"
var toObjectLongResult = jToken.ToObject<long>();
var valueLongResult = jToken.Value<long>();
// toObjectLongResult == valueLongResult == 123L
The JToken hierarchy looks like this: JToken - abstract base class JContainer - abstract base class of JTokens that can contain other JTokens JArray - represents a JSON array (contains an ordered list of JTokens) JObject - represents a JSON object (contains a collection of JProperties) JProperty - represents a JSON ...
JToken is an abstract base class that represents any one of these possible tokens. If you have some JSON and don't know in advance what might be inside, you can parse it with JToken. Parse() and get a result as long as the JSON is well-formed.
jToken refers to the “receipt” users get for supplying underlying assets when mining on JustLend, such as the jTRX, jUSDT, jSUN and jBTC you receive after supplying the corresponding assets. jToken is a TRC20 token in your wallet. JustLend mining pools are multi-reward pools jointly launched by SUN and JustLend.
JObject. It represents a JSON Object. It helps to parse JSON data and apply querying (LINQ) to filter out required data. It is presented in Newtonsoft. Json.
The difference is as follows:
ToObject<T>()
is a deserialization operation. It constructs a JsonSerializer
and uses it to deserialize the current JToken
to the desired type. As such the token could be anything (a JSON array, a JSON object, or a JSON primitive value) and the serializer will, using reflection, try to deserialize the token to the desired type by reading through its contents with a JTokenReader
.
This method is useful when writing generic code where the input token and output type could be anything. It is the most general and fail-safe way to create a c# object from a JToken
.
Extensions.Value<U>(IEnumerable<JToken>)
is a conversion/casting operation. It attempts to convert the value of the current token to the target type by invoking Convert.ChangeType()
(as well as handling a few special cases).
This method is useful when you know your JToken
is, in fact, a JValue
and you want to convert its Value
to a specific, required .Net primitive type. For instance, if the JValue
might contain a long
or numeric string, you could convert it to an int
, a decimal
or a double
. If it might contain a DateTime
or a string in ISO 8601 format, you could convert it to a DateTime
. And any primitive JSON value can always converted to a string.
While this method is less general than ToObject<T>()
it will be more performant in converting primitive values since the serializer invokes the same conversion methods internally when deserializing a primitive.
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