eadam
eadam

Reputation: 26181

JObject to dynamic not working in Newtonsoft.Json 9.0

There are many answers here which suggest the following ways to use JObject as dynamic.

dynamic dynObj = JObject.Parse(jsonString);
OR
dynamic dynObj = JsonConvert.DeserializeObject<dynamic>(jsonString);

My jsonString is simple: {"id":"123","name":"abc"}.

It does not seem to work in Newtonsoft.Json 9.0. When I try any of these, I still get an object with the Type object {Newtonsoft.Json.Linq.JObject}.

And when I try to access a property by doing dynObj.id I get an exception error CS1061: 'object' does not contain a definition for 'id' and no extension method 'id' accepting a first argument of type 'object' could be found.

Upvotes: 3

Views: 2490

Answers (1)

Brian Flynn
Brian Flynn

Reputation: 269

You are probably seeing the first chance exception being thrown in the debugger when the DLR first tries to bind to a property on the object. This exception can be safely ignored and if you continue running the code it should work fine.

see: Lots of first chance Microsoft.CSharp.RuntimeBinderExceptions thrown when dealing with dynamics

Upvotes: 8

Related Questions