0

I've got a JObject class and the members are dynamic, but I don't know how to access the dynamic member when the member name is stored in a variable.

Code is below.

 dynamic deserializedProduct = JObject.Parse(json);
 string[] user = emailBox.Text.Split('@');
 string pass = deserializedProduct.user[0].password;
 MessageBox.Show(pass);

// User[0] represents the member name

Any help would be greatly appreciated.

2 Answers2

0

if it is dynamic you should be able to access it as if it was just an property

deserializedProduct .user

but I guess you need the value inside the var user.

And in that case there are great utilities from westwind which extend the Expando objects.

check http://weblog.west-wind.com/posts/2012/Feb/08/Creating-a-dynamic-extensible-C-Expando-Object

Dan Kuida
  • 1,017
  • 10
  • 18
0

As mentioned by @Adriano in comment, JObject has string indexer. It is more suitable to use that indexer, instead of pushing to do this in common object property name style :

........  
string pass = ((dynamic)deserializedProduct[user[0]]).password;
//or full string indexer style
//string pass = deserializedProduct[user[0]]["password"];
MessageBox.Show(pass);
har07
  • 88,338
  • 12
  • 84
  • 137