From the standard, JSON is built out of the following five types of token:
- object: an unordered set of name/value pairs.
- array: an ordered collection of values.
- value: a string in double quotes, or a number, or
true
orfalse
ornull
, or an object or an array. These structures can be nested. - string
- number.
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. JObject.Parse()
and JArray.Parse()
will throw if the root JSON token is not of the expected type. And there is no JValue.Parse()
to parse a JSON string you know to represent an “atomic” value, requiring the use of JToken.Parse()
in such a case.
Similarly, JToken.FromObject()
may be used to serialize any sort of c# object to a JToken
hierarchy without needing to know in advance the resulting JSON type. This can be useful e.g. when writing generic serialization-related code.