1

I have a dynamic array contains JSON response and i'm trying to alter or add new child but it keeps giving me this error:

Additional information: 'System.Dynamic.DynamicObject' does not contain a definition for 'vault_data'

This is my code:

dynamic user = getData(apiKey); //return a dynamic JSON array .. vault_data contains a string
user.vault_data = "Trying to alter this child"; // here occur the error when I try to alter the child
user.data = "Trying to add new child"; // this will through the same error

Image:

enter image description here

Cyb3r
  • 59
  • 9
  • It may be a key-value pair. Try: `user["vault_data"] = "Trying to alter this child";` – Jason Faulkner Dec 21 '15 at 16:36
  • @JasonFaulkner Unfortunately it didn't work `Additional information: Cannot apply indexing with [] to an expression of type 'System.Dynamic.DynamicObject'`, I can see the child contents like this `MessageBox.Show(user.vault_data);` that's why I'm confused why my method didn't work. – Cyb3r Dec 21 '15 at 16:46
  • This is the drawback of using dynamic or var, the type is rather unclear. However, I see in your image that the vault_data is in some sort of `Array`. Is `user` an array? – Ian Dec 22 '15 at 09:38
  • @Ian Sorry for the late reply, yes it's an object array contains JSON response, any way around to alter or add new child? – Cyb3r Dec 24 '15 at 07:49

1 Answers1

1

Merry Christmas!

I think the best way to solve this is if you can debug on run-time just before the error happened to see what is the type assigned to the dynamic user. This info will be very useful for you to determine how to extract the data you want.

But since, at least from the image, we can see the user is an array, you should access it by first using the number index user[46] before you can access its members (which contains vault_data).

After that

  1. If the member is actually a KeyValuePair (KVP) with "vault_data" being the key, you should try to give second index in key form user[46]["vault_data"] to access the value.
  2. If it is not KVP, but a variable member, you can try to access it directly user[46].vault_data.
  3. If it is, in fact, another array, then you may need to access the access it by second index in number form user[46][1] (user[46][0] being the vault_data and thus user[46][1] is the value you want to change).
  4. And if it is a string, then you need to further parse user[46]

In all possibilities, the key is to access the member first by indexing the user array.

Ian
  • 30,182
  • 19
  • 69
  • 107
  • Thank you so much man for trying to help, and yeah it's KVP I have tried all the methods without any luck, I think it's read only, here's the class i'm using to parse json response http://stackoverflow.com/a/3806407/3785047. – Cyb3r Dec 28 '15 at 16:49
  • I see, it is read-only. The indexing would make you able to read it but not to write anything to it, is it? – Ian Dec 28 '15 at 16:51
  • Correct, and how could I override that? – Cyb3r Dec 28 '15 at 17:20
  • I have used the class in this answer http://stackoverflow.com/a/8333429/3785047 and it's working perfectly so far, Thanks again for trying to help man! – Cyb3r Dec 28 '15 at 23:51
  • 1
    Great! Glad to know that. You are welcome! Hope you can proceed well. ;) – Ian Dec 29 '15 at 02:08