c# – Linq – JToken, JObject, JProperty, JArray

c#

The JToken hierarchy looks like this:

JToken – is an abstract base class

JContainer – is an abstract base class of JTokens that can contain another 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 propety ( a name/JToken pair inside a JObject)

JValue — represent a primitive JSON value (string, bool,  number, null)

 

Here’s the basic rule of thumb:

  • If you know you have an object (denoted by curly braces { and } in JSON), use JObject
  • If you know you have an array or list (denoted by square brackets [ and ]), use JArray
  • If you know you have a primitive value, use JValue
  • If you don’t know what kind of token you have, or want to be able to handle any of the above in a general way, use JToken. You can then check its Type property to determine what kind of token it is and cast it appropriately.

 

Source: https://stackoverflow.com/questions/38558844/jcontainer-jobject-jtoken-and-linq-confusion

About: tientham

Software Engineer, SW Integrator & Mobile , Full Stack Developer


One thought on “c# – Linq – JToken, JObject, JProperty, JArray”

Leave a Reply

Your email address will not be published. Required fields are marked *