Reputation: 1538
I have a a JObject and I would like to set a property from a strongly typed object on it.
JObject["ProductionVersion"] = new ProductionVersion();
In order to do this, ProductVersion needs to be converted to a JToken. How can I do this without having to serialize and deserialize the object as a JObject?
JObject["ProductVersion"] = JsonConvert.DeserializeObject<JObject>(JsonConvert.SerializeObject(message.ProductVersion))
Upvotes: 16
Views: 23937
Reputation: 68670
Your question is a bit confusing..
JObject
and you want a JToken
?
Well, a JObject
is a JToken
. Take a look at the inheritance hierarchy here: JObject class
JToken.FromObject(obj)
Upvotes: 40
Reputation: 156978
You can access the properties by calling Properties
on the JObject
.
When you need to add a property, just add it using the JProperty
constructor.
See the JSON.NET documentation.
Upvotes: 1