How to read a value in Dynamic object obtained from JsonConvert.DeserializeObject

576 Views Asked by At

I am using httpClient to an API endpoint. There are a couple of possible JSON results I could get back depending on if the call if successful or not.

So I want to use a dynamic or ExpandoObject to DeserializeObject the results to.

However, I can't seem to be able to actual get anything from the object after Deserializing. I keep getting `

error CS1061: 'object' does not contain a definition for 'success' and no accessible extension method 'success' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)'

Here is a simplified example:

dynamic stuff = JsonConvert.DeserializeObject<ExpandoObject>("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");

When I try to access name like this stuff.name I get the following error:

error CS1061: 'object' does not contain a definition for 'name' and no accessible extension method 'name' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) ? stuff["name"] error CS0021: Cannot apply indexing with [] to an expression of type 'object'

And yet the data is in there somewhere:

enter image description here

If I Deserialize like this I get the exact same result:

dynamic stuff = JsonConvert.DeserializeObject("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");

It was inferred in another post that it may have something to do with this being in an async function, but no real solution. I am running .Net core 3.1 from within an Azure function.

So how do I refer to the object to get a value out?

0

There are 0 best solutions below