0

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?

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Brad Mathews
  • 1,567
  • 2
  • 23
  • 45
  • 2
    are the property names case sensitive? You have stuff.name, but the key is Name – Chris Strickland Aug 11 '21 at 00:08
  • 2
    The property name is `Name` not `name` -- capitalization matters. Anyway `ExpandoObject` implements `IDictionary` so you can loop though or search for members that way, see [How to dynamically get a property by name from a C# ExpandoObject?](https://stackoverflow.com/q/19139417/3744182). – dbc Aug 11 '21 at 00:08
  • Oops. Made a mistake on this test data. But no difference. Still get the error. – Brad Mathews Aug 11 '21 at 00:25
  • @dbc, Ok that worked: `var byName = (IDictionary)stuff; string val = (string)byName["Name"];` and for nested objects: `var address = (IDictionary)stuff.Address; string city = (string)address["City"];` Pretty clumsy, but works. – Brad Mathews Aug 11 '21 at 00:38
  • `stuff.Name` works also, see https://dotnetfiddle.net/wA9SXA. Not sure where your problem is. – dbc Aug 11 '21 at 01:28
  • @dbc, Yeah, neither do I, but some reason this works for most but not for some and the reason is still elusive. – Brad Mathews Aug 11 '21 at 04:11
  • out of context question - any specific reason to use single quote instead of double quote? Yes, you can use single quote in JSON but don't think it is a standard for JSON structure. @BradMathews - do you get the same error for accessing `stuff.Name` if JSON string is having double quotes? – user1672994 Aug 11 '21 at 04:30
  • @user1672994, My actual data uses double quotes and it really doesn't matter with JSON for most applications (I have had a few complain about single quoted). The reason to single quotes is it is much easier than escaping a bunch of double quotes. when making a json string. Also, this line was taken verbatim from another post on how to reference dynamic variables so was using an example that was supposed to work but does not for me. – Brad Mathews Aug 11 '21 at 19:13

0 Answers0