Converting JObject to a dynamic object
It is actually quite easy. Instead of using var
use dynamic
on your JObject
and you will be fine:
dynamic do = myObject.MyDynamicProp;
string name = do.Name;
From your fragment:
dynamic d = JsonConvert.DeserializeObject("{\"MyDynamicProp\": {\"id\": \"MyId2134\", \"Name\": \"MyName\"}}");
string name = d.MyDynamicProp.Name;
Console.WriteLine(name); // writes MyName
Why this works: As Richard explained, JObject
derives indirectly from JToken
which implements IDynamicMetaObjectProvider
. It is that interface that allows dynamic
to work.
doorman
Updated on April 17, 2022Comments
-
doorman about 2 years
I am calling a REST endpoint from C# and I am receiving json which gets serialized into an object. One of the properties on this object is a dynamic property. The value of the dynamic property is set as a anonymous object on the server site like this:
myObject.MyDynamicProp = new { Id = "MyId2134", Name = "MyName" };
On the client site the value of the dynamic property from the json serialization is a JObject containing the following value:
{{ "id": "MyId2134", "Name": "MyName" }}
I would have expected to be able to access the properties like this:
var s = myObject.MyDynamicProp.Name;
but it does not find the Name property instead I have to get the value like this:
var s = myObject.MyDynamicProp["Name"].Value;
I tried converting the JObject into a dynamic object like this but it returns JObject:
var dyn = myObject.MyDynamicProp.ToObject<dynamic>();
How can I convert the dynamic property value such that I can call its properties directly?
var s = myObject.MyDynamicProp.Name;
UPDATE ...
I ran the following
dynamic d = JsonConvert.DeserializeObject("{\"MyDynamicProp\": {\"id\": \"MyId2134\", \"Name\": \"MyName\"}}"); string name = d.MyDynamicProp.Name;
Which gives me the following the error:
{Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: `Newtonsoft.Json.Linq.JObject' does not contain a definition for `MyDynamicProp' at Microsoft.Scripting.Interpreter.ThrowInstruction.Run (Microsoft.Scripting.Interpreter.InterpretedFrame frame) [0x00027]
I would like to add that this is an Xamarin iOS project and the code is located in a PCL library.
I assumed there was a problem with my code but it looks like it is not possible to use dynamic types within a Xamarin iOS project. https://developer.xamarin.com/guides/ios/advanced_topics/limitations/
-
Richard about 8 yearsWhy this works:
JObject
derives indirectly fromJToken
which implementsIDynamicMetaObjectProvider
. It is that interface that allowsdynamic
to work. -
Patrick Hofman about 8 yearsYes, indeed. Thanks Richard. Can I append that to my answer?
-
doorman about 8 yearsThanks for your quick reply but even if I use dynamic instead of var I get "Unknown member: Name" when I call the property.
-
Patrick Hofman about 8 yearsIt is
Name
, notname
. -
doorman about 8 yearsThe precise exception I get is {Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:
Newtonsoft.Json.Linq.JObject' does not contain a definition for
Name' ... -
doorman about 8 yearsThanks for your suggestions. Please see my updated code. I also added that this is an Xamarin iOS project and the code is running within a PCL library.
-
Patrick Hofman about 8 yearsOkay, that could be a reason indeed. I have no experience on that. It does seem to come a long way, so I think it should be possible.
-
Patrick Hofman about 8 yearsCan you give it a try with a regular
dynamic
variable?