Serialize an object for a JToken

I have a JObject, and I would like to set a property from a strongly typed object on it.

JObject["ProductionVersion"] = new ProductionVersion(); 

To do this, ProductVersion must be converted to a JToken. How can I do this without having to serialize and deserialize an object as a JObject?

 JObject["ProductVersion"] = JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(message.ProductVersion)) 
+7
source share
2 answers

Your question is a bit confusing.

  • So, do you have a JObject and want a JToken ? Well, a JObject is a JToken . Take a look at the inheritance hierarchy here: JObject class
  • If you meant "I have a serializable object and I want to convert it to a JToken without having to serialize and deserialize it," use this JToken.FromObject(obj)
+21
source share

You can access properties by calling Properties in a JObject .

When you need to add a property, just add it using the JProperty constructor.

See the JSON.NET documentation.

+1
source share

All Articles